01-09-2016, 04:27 PM
Here we are, connected to the database. Woo hoo! But what good is that if we can’t get anything out of the database?
Solutions
PDO provides a couple of ways for us to interact with the database. Here, we’ll explore both possible solutions.
Using the Query Method
First, let’s look at the faster, but not necessarily better, way—using the query method:
Solutions
PDO provides a couple of ways for us to interact with the database. Here, we’ll explore both possible solutions.
Using the Query Method
First, let’s look at the faster, but not necessarily better, way—using the query method:
Quote:$country = 'USA';
try
{
$dbh = new PDO($dsn, $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
$sql = 'Select * from city where CountryCode =' .
$dbh->quote($country);
foreach (
$dbh->query($sql)
as $row)
{
print $row['Name'] . "\t";
print $row['CountryCode'] . "\t";
print $row['Population'] . "\n";
}
}
catch (PDOException $e)
{
echo 'PDO Exception Caught.
';
echo 'Error with the database: <br />';
echo 'SQL Query: ', $sql;
echo 'Error: ' . $e->getMessage();
}