why we use pdo in php

 


In this post, we will discuss PDO. We will talk about what is PDO, why we use the PHP PDO, How to use the PDO. We will discuss how does it help in PHP programming. we will understand the query structure in PDO. so if you are working on the project in PHP and want to use PDO this blog helps you to understand the query structure. And even you can pick the query for starting projects. So let's see PDO.

What is PDO in PHP?

PDO stands for PHP data object. Basically, it uses to deal with Databases. Before PDO we use MySQLI to deal with Databases. We use MySQLI to connect with the Database, read and update the Database.
Mysqli is still working but using PDO is the best practice to deal with Databases. For example, in MySQLI there is a high chance of performing the SQL injection and it can be work in MySQLI. But in PDO increases the security level. 


Why we use PDO in PHP?

There is a lot of databases Management system available in the world. Suppose you are working with the MYSQL database and after some time you have to change the database management system and switch to the POSTGRE and if you are using mysqli to connect, read or update your database. Then you have to write again all the SQL code of Postgre because the query structure is very different in both of the databases. But using PDO can help you to easily switch the database because both are Support the PDO(PHP Data Object) and in PDO the query structure is the same. You only have to change some names that use by the previous database. In simple words, you can  PDO is best for dealing with the database.

How to use the PDO in PHP?

First, let's see How to connect with the database using PDO in PHP. To connect with the database in PHP we have to create a PDO object. And its syntax looks like.

Database Connectivity using PDO Syntex

$server = "localhost";
$username =  "root";
$password =  "password";
$db =  "Database Name"; // change the Database Name with your database name.

$db_conn new PDO("mysql:host=localhost;dbname=$db","root","password"););

and it will be connected now let's see its query structure. and how to implement it in the code.

Insert Query In PHP PDO

$insert_query = "INSERT INTO database_name (column_name1, column_name2) VALUES (?,?)";

//helps to prepare the query for performing the action. 
$stmt = $db_conn->prepare($insert_query); 

//helps to execute that query
$stmt->execute([values1,values2]);

Select Query in PHP PDO

$select_query = "select column names from database_name where column names = ?"

//helps to prepare the query for performing the action. 
$stmt = $db_conn->prepare($select_query); 

//helps to execute that query
$stmt->execute([values1]);

Update Query in PHP PDO

$update_query = "Update table_name SET column name1 = 'value1', column_name2 = 'value2' where column_mame = ?";

//helps to prepare the query for performing the action. 
$stmt = $db_conn->prepare($update_query); 

//helps to execute that query
$stmt->execute([values1]);

Delete Query in PHP PDO

$delete_query = "Delete From database_name where column names = ?"

//helps to prepare the query for performing the action. 
$stmt = $db_conn->prepare($delete_query); 

//helps to execute that query
$stmt->execute([values1]);

Comments