PHP TutorialsWeb Development

PDO (PHP Data Objects) -How to bind Values in query ?

Before starting you must know What PDO is ? . PDO stands for PHP Data Objects. Its the new version to connect to database almost similar to Mysqli. Its support SQL Injection security etc. So lets start the topic How to bind Values  ? 

There are lot of ways to bind Values in PDO. 

We will discuss here the simple way to use bind Values in PDO.

First create a connection using PDO as shown below

$pdo = newPDO("mysql:host=localhost.com;dbname=data", 'username', 'password');

Then if you want to get data from a table abc  for a specific id then you have to define parameters as shown below.

$get_data = $pdo->prepare("Select * from abc where id = :id");

After define parameters like “id” you can bind value to parameter as shown below

$get_data = $pdo->prepare("Select * from abc where id = :id");

$get_data->bindValue("id", VALUE);

Now you can execute the query as shown below

$get_data = $pdo->prepare("Select * from abc where id = :id");

$get_data->bindValue("id", VALUE);

$get_data->execute();

This is simple way to bind Value in PDO . If you want to insert data in table abc by binding values then you have to define specific parameters for each column in which you want to insert data.

Lets suppose table abc has 2 columns name and class. Then we can insert values like this.

$insert_data = $pdo->prepare("INSERT INTO abc (name,class) VALUES (:namevalue ,:classvalue) ");

$insert_date->bindValue("namevalue", VALUE);

$insert_date->bindValue("classvalue", VALUE);

$insert_date->execute();

Remember parameters name can be whatever you want to use. during binding same parameters name will be used to assign values. If you define parameter name then same name will be use in binding.

Anil Mehra

I am Anil Mehra, a passionate, workaholic and a Full Stack Tech Savvy Programmer with true north towards growth. I have worked on 256 live projects in MNC. I am expertise in the field of Programming, Server Management, SEO, Blogging and SMO...

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button

Adblock Detected

Please turn off Ad blocker to view website