PHP Pdo
PHP Pdo
#pdo
Table of Contents
About 1
Remarks 2
Examples 2
Installation or Setup 2
Summary 2
Tutorials: 3
Remarks 7
Examples 7
Usage 7
Credits 9
About
You can share this PDF with anyone you feel could benefit from it, downloaded the latest version
from: pdo
It is an unofficial and free pdo ebook created for educational purposes. All the content is extracted
from Stack Overflow Documentation, which is written by many hardworking individuals at Stack
Overflow. It is neither affiliated with Stack Overflow nor official pdo.
The content is released under Creative Commons BY-SA, and the list of contributors to each
chapter are provided in the credits section at the end of this book. Images may be copyright of
their respective owners unless otherwise specified. All trademarks and registered trademarks are
the property of their respective company owners.
Use the content presented in this book at your own risk; it is not guaranteed to be correct nor
accurate, please send your feedback and corrections to [email protected]
https://riptutorial.com/ 1
Chapter 1: Getting started with pdo
Remarks
This section provides an overview of what pdo is, and why a developer might want to use it.
It should also mention any large subjects within pdo, and link out to the related topics. Since the
Documentation for pdo is new, you may need to create initial versions of those related topics.
Examples
Installation or Setup
PDO is a universal database connection command in PHP, it support 12 different database type
e.g MySQL, MongoDB, NoSQL. A big bonus about PDO is that it calculate your code to support
the database type, so you don't need to make any chance when moving over to another database
system.
Summary
PDO MySQLi
https://riptutorial.com/ 2
and useful, it's also help to make the secure part even easier.
Tutorials:
Install:
Guides:
W3Schools tutorial
Tuts+ Tutorial (Recommended)
PDO::ATTR_ERRMODE: This attribute is used for error reporting. It can have one of the following
values.
https://riptutorial.com/ 3
the default value of ATTR_ERRMODE attribute. It sets error codes. In silent mode, if there is
an error in SQL, PDO will throw no exceptions; PDO will issue no warnings; it will simply
return false. Value of PDO::ERRMODE_SILENT is 0. The script will run without generating
any error or warning.
• PDO::ERRMODE_WARNING: This value raises E_WARNING. In warning mode, if there is an error in
SQL, PDO will issue warnings but script will continue running. Value of
PDO::ERRMODE_WARNING is 1. The script will run with generating warning about the
error.
• PDO::ERRMODE_EXCEPTION: This value throws exceptions. In exception mode, if there is an error
in SQL, PDO will throw exceptions and script will stop running. Value of
PDO::ERRMODE_EXCEPTION is 2. The script will stop executing generating the error
which throws the exception.
Example: Now we are going to see the various values of the attribute ATTR_ERRMODE with
some examples. To do so, create a database called learn_project_db and insert a table called
user_table inside it. The following SQL snippet can be used to achieve so:
At first we are going to check what will happen if we do not set the ATTR_ERRMODE and we will have
error in the SQL query. Create a PHP file called default.php and try this:
<?php
$server = "localhost";
$db_username = "root";
$db_password = "";
$db_name = "learn_project_db";
$conn = new PDO("mysql:host=$server;dbname=$db_name",$db_username,$db_password);
$sql_query = "SELECT * FROM wrong_user_table";
$stmt = $conn->prepare($sql_query);
$stmt->execute();
$result_set = $stmt->fetchAll();
var_dump($result_set);
/*Get the current error mode of PDO*/
$current_error_mode = $conn->getAttribute(PDO::ATTR_ERRMODE);
echo "<br>";
echo "Value of PDO::ATTR_ERRMODE: ".$current_error_mode;
?>
Notice that, the table name is wrong_user_table in the query which is not defined in the database
we created earlier. But, as we did not set the ATTR_ERRMODE it will run the script without throwing any
exception or issuing any warning. It will output an empty array as result set. We should also notice
that, the value of PDO::ATTR_ERRMODE is 0.
https://riptutorial.com/ 4
Now, we are going to check what will happen if we set the ATTR_ERRMODE with value
PDO::ERRMODE_WARNING and we will have error in the SQL query. Create a PHP file called warning.php
and try this:
<?php
$server = "localhost";
$db_username = "root";
$db_password = "";
$db_name = "learn_project_db";
$conn = new PDO("mysql:host=$server;dbname=$db_name",$db_username,$db_password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$sql_query = "SELECT * FROM wrong_user_table";
$stmt = $conn->prepare($sql_query);
$stmt->execute();
$result_set = $stmt->fetchAll();
var_dump($result_set);
/*Get the current error mode of PDO*/
$current_error_mode = $conn->getAttribute(PDO::ATTR_ERRMODE);
echo "<br>";
echo "Value of PDO::ATTR_ERRMODE: ".$current_error_mode;
?>
Warning: PDOStatement::execute(): SQLSTATE[42S02]: Base table or view not found: 1146 Table
'learn_project_db.wrong_user_table' doesn't exist in E:\xampp\htdocs\oop\db.php on line 10
array(0) { }
Value of PDO::ATTR_ERRMODE: 1
This time as we set the ATTR_ERRMODE with PDO::ERRMODE_WARNING value it will show a warning
message. The script runs successfully and shows an empty array as output with the value of
PDO::ATTR_ERRMODE of 1.
At last, we are going to check what will happen if we set the ATTR_ERRMODE with value
PDO::ERRMODE_EXCEPTION and we will have error in the SQL query. Create a PHP file called error.php
and try this:
<?php
$server = "localhost";
$db_username = "root";
$db_password = "";
$db_name = "learn_project_db";
$conn = new PDO("mysql:host=$server;dbname=$db_name",$db_username,$db_password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql_query = "SELECT * FROM wrong_user_table";
$stmt = $conn->prepare($sql_query);
$stmt->execute();
$result_set = $stmt->fetchAll();
var_dump($result_set);
/*Get the current error mode of PDO*/
$current_error_mode = $conn->getAttribute(PDO::ATTR_ERRMODE);
echo "<br>";
echo "Value of PDO::ATTR_ERRMODE: ".$current_error_mode;
?>
https://riptutorial.com/ 5
Output of error.php is:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or
view not found: 1146 Table 'learn_project_db.wrong_user_table' doesn't exist' in
E:\xampp\htdocs\oop\db.php:10 Stack trace: #0 E:\xampp\htdocs\oop\db.php(10): PDOStatement-
>execute() #1 {main} thrown in E:\xampp\htdocs\oop\db.php on line 10
This time as we set the ATTR_ERRMODE with PDO::ERRMODE_EXCEPTION value it will throw PDOException
which generates a fatal error. The script will stop executing after the exception is thrown. This is
most common approach to handle database query related errors. In most of the time, we set the
attribute ATTR_ERRMODE with this value to handle any exception that might resides in SQL query.
https://riptutorial.com/ 6
Chapter 2: Prepare and Execute your SQL
statements
Remarks
Warning
Prepared statement cannot care a wild parameter for the table names. For exemple this following
statement is not correct :
$query = "SELECT name, city FROM users WHERE id = ? AND country = ?";
Examples
Usage
https://riptutorial.com/ 7
$data_1 = $statement_1->fetchAll();
$data_2 = $statement_2->fetchAll();
https://riptutorial.com/ 8
Credits
S.
Chapters Contributors
No
https://riptutorial.com/ 9