Prev | Current Page 76 | Next

Emilian Balanescu and Cristian Darie

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

mysql_error());
}
// Execute SQL query
$queryString = 'SELECT * FROM product';
$result = mysql_query($queryString);
if (!$result)
{
die ('Query failed : ' . mysql_error());
}
// Close connection
mysql_close($link);
CHAPTER 2 ?–  LAYING OUT THE FOUNDATIONS 24
?– Note If you are still planning to use PHP MySQL extension instead of PDO in your projects, you should
consider using the PHP MySQL improved extension (mysqli). You can find more details about the PHP MySQL
improved extension at http://www.php.net/manual/en/ref.mysqli.php.
Next, the same action is shown, this time using PDO:
try
{
// Create a new PDO instance
$database_handler =
new PDO('mysql:host=localhost;dbname=tshirtshop',
$username, $password);
// Build the SQL query
$sqlQuery = 'SELECT * FROM product';
// Execute SQL query
$statement_handler = $database_handler->query($sqlQuery);
// Fetch data
$result = $statement_handler->fetchAll(PDO::FETCH_ASSOC);
// Clear the PDO object instance
$database_handler = null;
}
catch (PDOException $e)
{
/* If something goes wrong we catch the exception thrown by
the object, print the message, and stop the execution of
script */
print 'Error!
' . $e->getMessage() . '
';
exit;
}
The version of the code that uses PDO is longer, but it includes a powerful error-handling
mechanism??”a very helpful tool when debugging your application If these concepts sound
foreign, once again, wait until the later chapters where we??™ll put PDO to work, and you??™ll learn
more about it there.


Pages:
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
zasady pokera imprezy integracyjne dla firm teksty piosenek Imprezy firmowe przeprowadzki wrocław