Php-mysql Course

About try ... catch()

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.
- getCode() - returns the error code.
- getMessage() returns the error message.
If these methods are not added, it can be displayed only a custom message.

setAttribute

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.
- OPTION - is the option /constant set for that attribute:


• Here's some examples with setAttribute(). It will be used the same "sites" table, created in the previous lessons.

- The next example sets the PDO::ATTR_CASE attribute with the PDO::CASE_UPPER option.
<?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:
- ID - NAME - CATEGORY - LINK

- The next example uses the setAttribute() to output the errors in the standard mode returned by PHP. It sets the PDO::ATTR_ERRMODE with the PDO::ERRMODE_WARNING option. To demonstrate the result, it is performed an SQL SELECT with a column that not exist in the "sites" table.
<?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:
Warning: PDO::query() [pdo.query]: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'nocolumn' in 'field list' in E:\server\www\test.php on line 19
Warning: Invalid argument supplied for foreach() in E:\server\www\test.php on line 22
- If you replace the ERRMODE_WARNING option with ERRMODE_EXCEPTION, the error message will be:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'nocolumn' in 'field list'

beginTransaction and commit

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.


In the next example it is used beginTransaction(), and three SQL commands: UPDATE (to modify data in the row with id=3), INSERT (to add a new row), and SELECT (using the last inserted "id", auto-created by the INSERT). All these instructions will be executed when the commit() method is called (see also the comments in the code).
<?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.
Between SQL commands which are "pending", you can add various PHP instructions to influence the next command (such as SELECT here was defined according to the latest "id" created by the previous query).
- The script above will output this result:
Last inserted id: 4
Courses - Tutorials - https://coursesweb.net
PHP-MySQL Course - https://coursesweb.net/php-mysql
Spanish Course - marplo.net/spaniola

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.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
PHP PDO - setAttribute, beginTransaction and commit

Last accessed pages

  1. Read Excel file data in PHP - PhpExcelReader (97351)
  2. Select in MySQL, Output results in HTML Table (19495)
  3. The Mastery of Love (7438)
  4. CSS cursor property - Custom Cursors (6091)
  5. Disable button and Enable it after specified time (17531)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (472)
  2. CSS cursor property - Custom Cursors (79)
  3. The Mastery of Love (70)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (62)
  5. CSS3 2D transforms (46)