note 106333 deleted from ref.pdo-mysql by danbrown
| From: | danbrown@php.net | Date: | Fri, 28 Oct 2011 23:32:15 +0000 |
| Subject: | note 106333 deleted from ref.pdo-mysql by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-184205@lists.php.net to get a copy of this message | ||
Note Submitter: marcus dot clarke at ghl dot com dot jm
----
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();
}
?>