Prev | Current Page 157 | Next

Emilian Balanescu and Cristian Darie

"Beginning PHP and MySQL E-Commerce: From Novice to Professional, Second Edition"


??? $db_pass represents the user??™s password.
??? $db_host is the hostname of your MySQL server.
??? $db_name is the name of the database you??™re connecting to.
??? $persistent is true if we want to create a persistent database connection or false
otherwise.
To disconnect from the database, you need to make $dbh equal null ($dbh = null).
The following code snippet demonstrates how to create, open, and then close aMySQL
database connection and also catch any exceptions that are thrown:
try
{
// Open connection
$dbh = new PDO('mysql:dbname=' . $db_name . ';host=' . $db_host,
$db_user, $db_pass);
// Close connection
$dbh = null;
}
CHAPTER 4 ?–  CREATING THE PRODUCT CATALOG: PART 1 87
catch (PDOException $e)
{
echo 'Connection failed: ' . $e->getMessage();
}
The try and catch keywords are used to handle exceptions.
PHP 5 EXCEPTION HANDLING
In Chapter 3, you implemented the code that intercepts and handles (and eventually reports) errors that happen
in the TShirtShop site. PHP errorsare the standard mechanism that you can use to react with an error
happening in your PHP code. When a PHP error occurs, the execution stops; you can, however, define an
error-handling function that is called just before the execution is terminated. You added such a function in
Chapter 3, where you obtained as many details as possible about the error and logged them for future reference.


Pages:
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
Pompy zanurzeniowe pisanie prac licencjackich hotele praga House Extension Kąty Rybackie noclegi