Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I fetch data from a table?
#1
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:


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();
}
Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  What is managed code and managed data? beingchinmay 0 2,336 01-02-2016, 03:29 PM
Last Post: beingchinmay

Forum Jump:


Users browsing this thread: 1 Guest(s)