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 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;
Here i am writing the statement for the INSERT query.
$query = $DBH->query("INSERT INTO `users` (`name`, `username`, `email`) values ('$name', '$username', '$email')");
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'");
Here i am writing the statement for the DELETE query.
$query = $DBH->query("DELETE FROM `users` WHERE id = '$id'");
My name is Mukesh Jakhar and I am a Web Application Developer and Software Developer, currently living in Jaipur, India. I have a Master of Computer Application in Computer Science from JNU Jaipur University. I loves to write on technology and programming topics. Apart from this, I love to travel and enjoy the beauty of nature.