php - Warning: Invalid argument supplied for foreach() when retrieving data from database -
i attempting retrieve data 2 tables , echo results out, sql appears correct tells argument invalid. heres code:
// retrieve information related post function get_post_data($post_id){ //test connection try{ //connect database $dbh = new pdo("mysql:host=localhost;dbname=mjbox","root", "usbw"); //if there error catch here } catch( pdoexception $e ) { //display error echo $e->getmessage(); } $sql = 'select * mjbox_images join mjbox_posts using (post_id) post_id = $post_id'; $result = $dbh->query( $sql ); foreach($result $row): echo $row['img_id']; endforeach; }
the $post_id in query won't being expanded because string single quoted.
it should work better with:
$sql = "select * mjbox_images join mjbox_posts using (post_id) post_id = $post_id"; or:
$sql = 'select * mjbox_images join mjbox_posts using (post_id) post_id = '.$post_id;
Comments
Post a Comment