note 106333 added to ref.pdo-mysql

From: Date: Fri, 28 Oct 2011 15:21:20 +0000
Subject: note 106333 added to ref.pdo-mysql
Groups: php.notes 
Request: Send a blank email to php-notes+get-184198@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/106333 Del: integrated -- https://master.php.net/note/delete/106333/integrated Del: useless -- https://master.php.net/note/delete/106333/useless Del: bad code -- https://master.php.net/note/delete/106333/bad+code Del: spam -- https://master.php.net/note/delete/106333/spam Del: non-english -- https://master.php.net/note/delete/106333/non-english Del: in docs -- https://master.php.net/note/delete/106333/in+docs Del: other reasons-- https://master.php.net/note/delete/106333 Reject -- https://master.php.net/note/reject/106333 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#184198) next »