php pdo queries statements select insert update delete

In last tutorial we discuss about PDO and how to create PDO connection file. In this tutorial i am going to teach you how to write down PDO query statements such as SELECT Single Row And Multiple Rows, INSERT, UPDATE Query And DELETE etc.

SELECT QUERY USING PHP PDO

Select table data into associate array format
Here i am creating the statement for select query fetch multiple rows in associate array format. Select data from user table.

$query = $DBH->query('SELECT * from users');
$query->setFetchMode(PDO::FETCH_ASSOC); 
while($row = $query->fetch()) {
    echo $row['id'] . " ";
    echo $row['username'] . "<br>";
}

Here i am creating the statement for select query fetch single rows in associate array format. Select data from user table where id of user is one (1).

$query = $DBH->query('SELECT * from users WHERE id='1'');
$query->setFetchMode(PDO::FETCH_ASSOC);
$row = $query->fetch();
echo $row['id'];
echo $row['username'];

Select table data into object format
Here i am creating the statement for select query fetch multiple rows in object format. Select data from user table.

$query = $DBH->query("SELECT * FROM  WHERE `users`"); 
while($row =$query->fetchObject())
{
    echo $row->id;
    echo $row->username . "<br>";
}

Here i am creating the statement for select query fetch single rows in object format. Select data from user table.

$query = $DBH->query("SELECT * FROM  WHERE `users`"); 
$row =$query->fetchObject();
echo $row->id;
echo $row->username;

INSERT QUERY USING PHP PDO

Here i am writing the statement for the INSERT query.

$query = $DBH->query("INSERT INTO `users` (`name`, `username`, `email`) values ('$name', '$username', '$email')");

UPDATE QUERY USING PHP PDO

Here i am writing the statement for the UPDATE query.

$query = $DBH->query("UPDATE `users` SET `name` = '$name', `username` = '$username', `email` = '$email' WHERE id = '$id'");

DELETE QUERY USING PHP PDO

Here i am writing the statement for the DELETE query.

$query = $DBH->query("DELETE FROM `users` WHERE id = '$id'");

Leave a Reply

Related Posts: You may like

How to remove whitespace from string using php

Remove All Special Characters From String PHP

comma separated values in mysql query

SQL statement to retrieve names beginning with A

Tools:

Crypto Currency

PHPKIDA provides free online the best real-time crypto market price, news, predictions, index with graphs & historical data for 2000+ coins including bitcoin, ethereum, & altcoins from major cryptocurrency exchanges.

Visite Tool

Icon Generator

PHPKIDA provides free online icon generator. Our icon generator help you to edit, modify, resize and customize icon for your website and applications. Convert to a shap you like add icon border, you can change icon border color, border size, background color, icon shadow, icons color, icon size in your own way and save it as a png image.

Visite Tool

Youtube Video Downloader

PHPKIDA provides Free online YouTube video downloader to download YouTube videos quickly in MP4, 3GP, and more. Search your video and free fast download youtube videos.

Visite Tool

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.