When it is created a PDO object with a connection to a database, in case of an error will throw the PDOException. If the error is not catched with try ... catch() PHP will stop the execution of the script.
PDOException is an extension of the PHP Exception class, that can "catch" the errors.
With try ... catch(), besides the fact that the error is taken and the script can continue its execution, it can also personalize the error message which will be displayed.
Syntax:
try { // ... PHP instructions } catch(PDOException $e) { echo 'Custom Error Message'; // Output the error code and the error message echo $e->getCode(). '-'. $e->getMessage(); }- $e - is the object that will store the error detected by PHP.
The setAttribute() method can be used to set various attributes to the PDO object that handles the connection to database, including how to report the errors catched with "try ... catch()".
Syntax:
$PDOobject->setAttribute(ATTRIBUTE, OPTION)- ATTRIBUTE - represents the attribute that will be set.
<?php // Connection data (server_address, database, name, poassword) $hostdb = 'localhost'; $namedb = 'tests'; $userdb = 'username'; $passdb = 'password'; try { // Connect and create the PDO object $conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb); $conn->exec("SET CHARACTER SET utf8"); // Sets encoding UTF-8 // Set the column names to be returned uppercase $conn->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER); // Select the first row $sql = "SELECT * FROM `sites` LIMIT 1"; $result = $conn->query($sql)->fetch(PDO::FETCH_ASSOC); // Execute query and fetch with FETCH_ASSOC // If the SQL query is succesfully performed ($result not false) if($result !== false) { // Traverse the result set and output the column names foreach($result as $col=>$row) { echo ' - '. $col; } } $conn = null; // Disconnect } catch(PDOException $e) { echo $e->getMessage(); }- This script will display the column names in uppercase:
<?php // Connection data (server_address, database, name, poassword) $hostdb = 'localhost'; $namedb = 'tests'; $userdb = 'username'; $passdb = 'password'; try { // Connect and create the PDO object $conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb); // Sets to handle the errors in the PHP standard mode (E_WARNING) $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); $conn->exec("SET CHARACTER SET utf8"); // Sets encoding UTF-8 // Select the first row $sql = "SELECT `nocolumn` FROM `sites` LIMIT 1"; $result = $conn->query($sql); // Executa interogarea // Traverse the result set and output data in the 'nocolumn' foreach($result as $row) { echo $row['nocolumn']; } $conn = null; // Disconnect } catch(PDOException $e) { echo $e->getMessage(); } ?>- Because "nocolumn" not exist, the code above will output this error:
The beginTransaction() method allows to write SQL statements without to be sent to MySQL server, it is used together with commit().
beginTransaction() stops the execution of any query to the database until the commit() method is called, in that moment will be executed all the SQL queries added between these two methods.
The advantage of this technique is that it can be written several sets of SQL queries, that are "pending", then, when the commit() method is called, all that SQL queries will be executed.
<?php // Connection data (server_address, database, name, poassword) $hostdb = 'localhost'; $namedb = 'tests'; $userdb = 'username'; $passdb = 'password'; try { // Connect and create the PDO object $conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Sets exception mode for errors $conn->exec("SET CHARACTER SET utf8"); // Sets encoding UTF-8 $conn->beginTransaction(); // Start writting the SQL commands // 1. Update the columns "name" and "link", in rows with id=3 $conn->exec("UPDATE `sites` SET `name`='Spanish Course', `link`='marplo.net/spaniola' WHERE `id`=3"); // 2. Add a new row $conn->exec("INSERT INTO `sites` (`name`, `category`, `link`) VALUES ('JavaScript', 'programming', 'coursesweb.net/javascript')"); $last_id = $conn->lastInsertId(); // Get the auto-inserted id // 3. Selects the rows with id lower than $last_id $result = $conn->query("SELECT `name`, `link` FROM `sites` WHERE `id`<'$last_id'"); $conn->commit(); // Determine the execution of all SQL queries // If the SQL select is succesfully performed ($result not false) if($result !== false) { echo 'Last inserted id: '. $last_id. '<br />'; // Displays the last inserted id // Traverse the result set and shows the data from each row foreach($result as $row) { echo $row['name']. ' - '. $row['link']. '<br />'; } } $conn = null; // Disconnect } catch(PDOException $e) { echo $e->getMessage(); } ?>- As shown in the example above, this method is useful when you have to execute several different queries to the database in the same script. Besides, improve the execution speed of the script, make it more efficient to work with multiple SQL queries.
If the ATTR_ERRMODE is set to ERRMODE_WARNING (with the setAttribute() method), and an error ocurs to one of the SQL instructions between beginTransaction() and commit(), the next SQL instructions after the query which has generated the error will not be executed.
But if it's not specified the ERRMODE_WARNING mode, the PHP continues the execution of the other queries too.
<table><tr> <th>Title 1</th> <th>Title 2</th> </tr></table>
.some_class { line-height: 150%; }
document.getElementById("id_button").onclick = function(){ window.open("http://coursesweb.net/"); }
$ar_dir = scandir("dir_name"); var_export($ar_dir);