How to redirect unattached image attachments

Posts that aren’t attached have no parent, and posts with no parent have a parent of 0.

So this:

if ( is_attachment() && isset( $post->post_parent ) && is_numeric( $post->post_parent ) && ( $post->post_parent != 0 ) ) {

becomes this:

if ( is_attachment() && ( $post->post_parent == 0 ) ) {

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)