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
What attribute makes a radio button or checkbox input selected?
checked="checked" selected="selected" disabled="disabled"
<input type="checkbox" name="a_name" value="value" checked="checked" />
What CSS value scales the background image to the largest size contained within the element?
repeat-x contain linear-gradient
#id {
  background:url("path_to_image.png");
  background-size:contain;
  background-repeat:no-repeat;
}
What operator is used to determine the rest of the division of two numbers?
% * /
var rest8_7 = 8 % 7;
alert(rest8_7);
Indicate the PHP function that rounds a number up to the next highest integer.
floor() ceil() abs()
$nr = ceil(3.5);
echo $nr;        // 4
PHP PDO - setAttribute, beginTransaction and commit

Last accessed pages

  1. A simple script ActionScript 3 (4416)
  2. Check the file type before Upload (9307)
  3. SHA1 Encrypt data in JavaScript (35501)
  4. Ajax-PHP File Manager (10274)
  5. Contact page - CoursesWeb (48922)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (70)
  2. Read Excel file data in PHP - PhpExcelReader (11)
  3. ActionScript 3 Lessons (7)
  4. The Mastery of Love (7)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (6)