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

Extract Total Number of Image And Missing Alt Attribute using php

How to extract Heading tags using PHP from string

How to remove whitespace from string using php

Remove All Special Characters From String PHP

PHPKIDA Offering Free Tools:

INSTA DOWNLOADER

Download Instagram Videos & Photos.

Download Now

CURRENCY CONVERTER

Real Time Currency Converter

Convert Now

ROBOST.TXT GEN.

Robots.txt File Creater

Create Now

HTML MINIFIER

Compress yout HTML Code

Minify Now

CSS MINIFIER

Compress yout CSS Code

Minify Now

JAVASCRIPT MINIFIER

Compress yout JavaScript Code

Minify Now

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.