MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Introduction to PHP Data Object PDO in PHP

There are three main API options when considering connecting to a MySQL database server.

  1. PHP’s MySQL Extension.
  2. PHP’s mysqli Extension.
  3. PHP Data Object (PDO).

PHP Data Object (PDO)

PDO is a database access layer that provides a fast and consistent interface for accessing and managing databases in PHP applications. Every DBMS has specific PDO drivers(s) that must be installed when you are using PDO in PHP applications. It supports only the Object-Oriented interface. PDO will work on 12 different database systems.

Benefits of PDO

  • Usability
  • Error Handling
  • Multiple Databases
  • Secure
  • Reusability
  • Exception Class

Supported Databases

  • CUBRID
  • MS SQL Server Sybase
  • Firebird
  • IBM
  • Informix
  • MySQL
  • MS SQL Server
  • Oracle
  • ODBC and DB2
  • PostgreSQL
  • SQLite
  • 4D

Difference and Similarities between MySQLi and PDO

Difference

  1. MySQLi supports dual interface Procedural and object-oriented while PDO supports only Object-oriented.
  2. MySQLi will work only with MySQL Database while PDO will work on 12 different database systems.

Similarities

  1. Both support Prepared Statements.
  2. Both provide the same level of security.
  3. Both supports Object-Oriented.

PDO – Fetch DB Data/SELECT

  • query($sql_statement) – It executes an SQL statement in a single function call, returning the result set(if any) returned by the statement as a PDO Statement object or FALSE on failure.
  • fetch($fetch_Style) – It fetches a row from a result set associated with a PDO Statement object. The fetch_style parameters determine how PDO returns the row.
  • fetchAll($fetch_Style) – It returns an array containing all of the remaining rows in the result set, The array represents each row as either an array of column values or an object with properties corresponding to each column name. An empty array is returned if there are zero results to fetch, or FALSE on failure. The fetch_style parameter determines how PDO returns the row.

$fetch Style

It controls how the next row will be returned to the caller.

PDO:: FETCH_BOTH(default): It returns an array indexed by both column name and 0-indexed column number as returned in your result set.

PDO::FETCH_ASSOC: It returns an array indexed by column name(associative array) as returned in your result set.

PDO::FETCH_NUM: It returns an array indexed by column number(index array) as returned in your result set, starting at column 0.

PDO::FETCH_OBJ: It returns an anonymous object with property names that correspond to the column names returned in your result set.

PDO::FETCH_NAMED: It returns an array with the same form as PDO:: FETCH_ASSOC, except that if there are multiple columns with the same name, the value referred to by that key will be an array of all the values in the row that had that column name.

rowCount()

It returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatment object. If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some database may return the number of rows returned by the statement.

Insert Data

exec($sql_statement) – It executes an SQL statement in a single function call, returning the number of rows affected by the statement. It does not return results from a SELECT statement.

Table structure for table student

CREATE TABLE student (
id int(255) NOT NULL,
name varchar(100) NOT NULL,
roll varchar(100) NOT NULL,
address varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

//Create Connection

$dsn = "mysql:host=localhost;dbname=test;";
$db_user = "root";
$db_password = "";

try{

 //Create Connection
 $conn = new PDO($dsn, $db_user, $db_password);
 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 echo "Connected  
";

 $name = 'JavaScript';
 $roll = '109';
 $address = 'Kolkata';

 // Insert Data
 $sql = "INSERT INTO student (name, roll, address) VALUES ('$name', '$roll', '$address')";
 $affected_row = $conn->exec($sql);
 echo $affected_row . "Row Inserted 
";  
}

catch(PDOException $e){
 echo "Connection Failed" .$e->getMessage();
}

?>

Insert Data into Form.

Retrieve and Insert Data using PDO in PHP.

Delete Data from Database using PDO in PHP.

setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	echo "Connected 
"; //Delete Data $sql = "DELETE FROM student WHERE id = 19"; $affected_row = $conn->exec($sql); echo $affected_row . "Row Deleted Successfuly "; } catch(PDOException $e){ echo "Connection Failed" .$e->getMessage(); } $conn = null; ?>

Delete Data from Database using Button PDO in PHP.

Update Data into Database using PDO in PHP.

setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	echo "Connected 
"; //Update Data $sql = "UPDATE student SET name = 'Sameer', roll = '110', address = 'Chennai' WHERE id = 15"; $affected_row = $conn->exec($sql); echo $affected_row . "Row Updated Successfuly "; } catch(PDOException $e){ echo "Connection Failed" .$e->getMessage(); } $conn = null; ?>

Update Data into Database using Form PDO in PHP.

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
MotoShare.in provides the perfect two-wheeler for every occasion—daily commuting, weekend escapes, tourist exploration, or test-riding your dream bike. Simplify your mobility with us!

Related Posts

Complete reference guide of PHP Databases!

PHP MySQL Introduction MySQL is one of the most popular relational database system being used on the Web today. It is freely available and easy to install, however if…

Read More

How to delete records?

1) Write the delete statement: [code language=”php”] $sql = "DELETE FROM `users` WHERE `id`=:id"; [/code] 2) Prepare the query: [code language=”php”] $query = $dbh -> prepare($sql); [/code]…

Read More

How to use PDO to update the database?

1) Write the regular update statement and again, instead of values, assign the named placeholders. For example: [code language=”php”] $sql = "UPDATE `users` SET `city`= :city, `phone`…

Read More

How to use PDO to read data from the database?

1) Write the regular select statement and again, instead of values, put named placeholders. For example: [code language=”php”] $sql = "SELECT * FROM users"; [/code] 2) Prepare…

Read More

How to use PDO to insert data into the database?

The SQL code for the users table: [code language=”sql”] CREATE TABLE IF NOT EXISTS users (id int(11) NOT NULL AUTO_INCREMENT, name varchar(60) DEFAULT NULL, phone varchar(12) DEFAULT…

Read More

PDO – PHP database extension

PDO – PHP database extension PDO (PHP Data Objects) is a PHP extension through which we can access and work with databases. Though PDO is similar in…

Read More