note 106334 added to ref.pdo-mysql

From: Date: Fri, 28 Oct 2011 15:22:11 +0000
Subject: note 106334 added to ref.pdo-mysql
Groups: php.notes 
Request: Send a blank email to php-notes+get-184199@lists.php.net to get a copy of this message
I am new to PDO_MYSQL but correct if I have missed step in the code snippet. My aim is to try to output the resultset from a (select query ) table named animal. My problem is when I try to execute and run the code nothing is printed from the resultset and no exception was thrown. <?php /*** mysql hostname ***/ $hostname = 'localhost'; /*** mysql username ***/ $username = 'root'; /*** mysql password ***/ $password = ''; try { $dbh = new PDO("mysql:host=$hostname;dbname=world", $username, $password); /*** echo a message saying we have connected ***/ echo 'Connected to database<br />'; /*** The SQL SELECT statement ***/ $sql = "SELECT * FROM animals"; foreach ($dbh->query($sql) as $row) { print $row['animal_type'] .' - '. $row['animal_name'] . '<br />'; } /*** close the database connection ***/ $dbh = null; } catch(PDOException $e) { echo $e->getMessage(); } ?> ---- Server IP: 208.87.36.210 Probable Submitter: 72.252.249.18 ---- Manual Page -- http://www.php.net/manual/en/ref.pdo-mysql.php Edit -- https://master.php.net/note/edit/106334 Del: integrated -- https://master.php.net/note/delete/106334/integrated Del: useless -- https://master.php.net/note/delete/106334/useless Del: bad code -- https://master.php.net/note/delete/106334/bad+code Del: spam -- https://master.php.net/note/delete/106334/spam Del: non-english -- https://master.php.net/note/delete/106334/non-english Del: in docs -- https://master.php.net/note/delete/106334/in+docs Del: other reasons-- https://master.php.net/note/delete/106334 Reject -- https://master.php.net/note/reject/106334 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#184199) next »