Solution:
globalize $wpdb before this
global $wpdb
$post_id = $wpdb->get_var( $wpdb->prepare("SELECT id FROM $wpdb->posts WHERE post_password = %s", $post_password) ); $q = new WP_Query( 'p=$post_id' );
Also it is a best practise to use lowercase table/column names
and then redirect like so
<?php if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post(); ?>
wp_redirect( the_permalink() );
<?php endif;?>
Also use a http status code as the second parameter to wp_redirect() This might be helpful HTTP STATUS CODES