Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

“Invest in yourself — your confidence is always worth it.”

Explore Cosmetic Hospitals

Start your journey today — compare options in one place.

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

What is CodeIgniter and use cases of CodeIgniter?

What is CodeIgniter? CodeIgniter is an open-source PHP web application framework designed to simplify and accelerate web development. It follows the Model-View-Controller (MVC) architectural pattern and provides…

Read More

What is PHP and use cases of PHP?

What is PHP? Are you wondering what PHP is and how it works? Look no further! In this article, we will explore the ins and outs of…

Read More

Complete guide on PHP certification courses, tutorials & training

What is PHP Hypertext Preprocessor is known as PHP. Many developers use PHP, an open-source server-side programming language, to create websites. In addition, it is a general-purpose…

Read More

Complete PHP with Laravel Certification Guide & tutorials

What is PHP with Laravel? PHP was visioned eventually in the fall of 1994 by Rasmus Lerdorf. PHP means Hypertext Preprocessor. It’s a extensively- used, open source…

Read More

Top 50 PHP Interview Questions and Answers

1) What is PHP? PHP is an open-source, interpreted, and object-oriented scripting language that can be executed at the server-side. PHP is well suited for web development….

Read More

Complete guide of PHP certification courses, tutorials & training

PHP is a great beginner programming language for anyone who is wanting to cross over into the coding world. Though easy to learn, it is an extremely…

Read More