21 September 2021
Read more Like this Post.
What's is pdo?
PDO mean (PHP Data Object). this is the block of code which about to interaction with databases or provide simplest way to access the database. this is the light weight way to access the database in this lesson we learn how to perform all task with PDO in database.

PHP logo from www.php.net

Benefits of PDO

  • Working with Multiple Database
  • Security / prepared statement
  • Usability
  • Excellent Error Handling

PDO Classes

  • PDO - this class make a connection between DB and PHP.
  • PDOStatement - represents a prepared statement and after executed an associated result.
  • PDOException - Reprsent errors raised by PDO.


  • mysql
  • ibm
  • postgreSQL
  • Informix
  • MS SQL Server SQlite
  • Oracle FreeTDS
  • FireBird
  • 4D
  • sybase 
  • cubird
  • Many others

lets jump into tutorial first install php in your machine I recommanded xampp or wamp.
next open php.ini from your php root folder look. after opening the ini file look almost in line 909
if the this line is comment first uncomment it because we use SQL if the user use other database all the database is available here just uncomment it.
now start the xampp then create a database in and a table in the database and put some data in table.
after creating database and creating table than start the coding in php file.
create some variable for connecting db.
	$host = 'localhost';
	$user = 'root';
	$dbname = 'myDataBaseName';

this is the variables now lets create a DSN.

Whats DSN?

DSN Stand for Data Source Name is a structer thats have  the information about a specific database.
	$dsn = 'mysql:host=' . $host . ';dbname=' . $db;
	//make a pdo class object 
	$pdo = new PDO($dsn, $user, $password);
$pdo is the instance of PDO class which take three parameters $dsn, $user and $password.
	$stmt = $pdo->query("SELECT * FROM posts");
above line is the query of fetching all elements from the DB table which store in $stmt variable the $pdo->query is the function of the PDO which apply the sql code to sql database.
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo '<br>';
    echo $row['title'];
after fetching all the data from database we just create a while loop and pass the array of the data. fetch function is used for fetching the data from database table in different forms. we can fetch the data in array, object, associative array, etc.
we can make a default fetching type like this
	$pdo = new PDO($dsn, $user, $password);
$stmt = $pdo->query("SELECT * FROM posts");
echo '<pre>';
while ($row = $stmt->fetch()) {
    echo '<br>';

in this code we create a default fetching method on line 2 we call a setAttribute function with some arguments in second parameter we say the default fetching method is OBJ type in while loop the fetch method is empty arguments this make all the fetching data in object type like $row->title is equal to $row['title'] one is object and second is array type data.
Related Post's

Not Loaded Zohaib Jozvi
0 21 September 2021
Read Latest Post.
Read other post.