In the last post we looked at accessing the data in a database table. This time we're going to make a small refinement by enabling that code to handle errors using try and catch.
In order to do this we first create our try statement and put our PHP connection and query code inside it.
Next we set up what happens if there's an error. (If the try fails we catch.)
The final code we need then to access the data we set up in the previous post with catch and try in place is:
We do this try and catch because the PHP documentation warns us to do so:
In order to do this we first create our try statement and put our PHP connection and query code inside it.
try {
// Create database connection using PHP Data Object (PDO)
$db = new PDO("mysql:host=localhost;dbname=test", $username, $password);
// Identify name of table within database
$table = 'age';
// Create the query - here we grab everything from the table
$stmt = $db->query('SELECT * from '.$table);
// Close connection to database
$db = NULL;
while($rows = $stmt->fetch()){
echo "<tr><td>". $rows['Name'] . "</td><td>" . $rows['Age'] . "</td></tr>";
};}
// Create database connection using PHP Data Object (PDO)
$db = new PDO("mysql:host=localhost;dbname=test", $username, $password);
// Identify name of table within database
$table = 'age';
// Create the query - here we grab everything from the table
$stmt = $db->query('SELECT * from '.$table);
// Close connection to database
$db = NULL;
while($rows = $stmt->fetch()){
echo "<tr><td>". $rows['Name'] . "</td><td>" . $rows['Age'] . "</td></tr>";
};}
Next we set up what happens if there's an error. (If the try fails we catch.)
catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
The final code we need then to access the data we set up in the previous post with catch and try in place is:
<html>
<table>
<tr>
<td>Name</td>
<td>Age</td>
</tr>
<?php
// Enter username and password
$username = root;
$password = root;
try {
// Create database connection using PHP Data Object (PDO)
$db = new PDO("mysql:host=localhost;dbname=test", $username, $password);
// Identify name of table within database
$table = 'age';
// Create the query - here we grab everything from the table
$stmt = $db->query('SELECT * from '.$table);
// Close connection to database
$db = NULL;
while($rows = $stmt->fetch()){echo "<tr><td>". $rows['Name'] . "</td><td>" . $rows['Age'] . "</td></tr>";
};}
catch (PDOException $e) { print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
</table>
</html>
<table>
<tr>
<td>Name</td>
<td>Age</td>
</tr>
<?php
// Enter username and password
$username = root;
$password = root;
try {
// Create database connection using PHP Data Object (PDO)
$db = new PDO("mysql:host=localhost;dbname=test", $username, $password);
// Identify name of table within database
$table = 'age';
// Create the query - here we grab everything from the table
$stmt = $db->query('SELECT * from '.$table);
// Close connection to database
$db = NULL;
while($rows = $stmt->fetch()){echo "<tr><td>". $rows['Name'] . "</td><td>" . $rows['Age'] . "</td></tr>";
};}
catch (PDOException $e) { print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
</table>
</html>
We do this try and catch because the PHP documentation warns us to do so:
And this is something we shouldn't ignore, because it risks exposing password and username data, along with other code we don't want a user to see.
Now test the error handling by breaking your code, e.g. use the wrong password, to see what happens.
Note: This post isn't an overview of PDO it just considers one element of it, and I'd recommend researching in full detail before implementing. For example, it is likely that you would want to use the full functionality of PDO, including prepare, execute, query and bind, in real-world use.
This comment has been removed by a blog administrator.
ReplyDeletePDO is a database access layer which provides a fast and consistent interface for accessing and managing databases in PHP applications. Every DBMS has specific PDO driver(s) that must be installed when you are using PDO in PHP applications.
ReplyDeleteSource: https://www.cloudways.com/blog/introduction-php-data-objects/