php code for image fetching

How to fetch the featured image in wordpress post[?]

echo get_the_post_thumbnail( $post_id ); 

Or, inside a proper Loop,

the_post_thumbnail();

But I have a feeling that there is information missing from the question. You aren’t using any Core functions and there is no explanation why, or reasons why not.

For example, WP_Query would be far easier that cooking up your own SQL. Using the $wpdb object would be easier than making your own MySQL connection. Using get_permalink is easier and more reliable than creating a link manually.

Reference

http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail
http://codex.wordpress.org/Function_Reference/the_post_thumbnail