Sunday, 12 January 2014

PHP MySQL Simple Select using PDO in Bootstrap Tutorial


 Source: Click here

Introduction

This tutorial is about PDO (PHP Data Objects) select statement for showing data from MySQL table on a PHP Page. We will use three various methods to select data from database using Prepared queries. We will learn
  • How to make a Connection Page.
  • How to prepare query for Select
  • How to bind values for query.
  • How to execute select query.

Connect to Database

First we will make configPDO.php file which use a PDO (PHP Data Objects) for connecting MySQL Database.
<?php
$hostname = 'localhost';
$username = 'root';
$password = '';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=YourDatabaseName", $username, $password);
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
?>

Prepare and Execute Select Queries

Now we will discuss how to prepare and execute PDO queries. We will use
fetchAll();
for getting all results.
<?php
include('configPDO.php');
$Fruitname    =  "Apple";     //Fruit name
$STM = $dbh->prepare("SELECT  `Fruitname`, FORMAT(AVG(`Rate`),2) FROM fruitsratelist WHERE Fruitname=:Fruitname");
$STM->bindParam(':Fruitname', $Fruitname);
$STM->execute();   
$STMrecords = $STM->fetchAll();
foreach($STMrecords as $row)
{
echo $row[0];
echo $row[1]; 
}
?> 
OR
We can also use just fetch
fetch();
Only gets one row. So no foreach loop needed.
<?php
include('configPDO.php');
$Fruitname    =  "Apple";     //Fruit name
$STM = $dbh->prepare("SELECT  `Fruitname`, FORMAT(AVG(`Rate`),2) FROM fruitsratelist WHERE Fruitname=:Fruitname");
$STM->bindParam(':Fruitname', $Fruitname);
$STM->execute();   
$STMrecords = $STM->fetch();
echo $row[0];
echo $row[1]; 
?> 

Prepared Queries Methods

There are three various methods to write PDO queries.
In this Demo we used named parameters key with prepared statement. Binding parameter is must before execution using this method.
$STM = $dbh->prepare("SELECT  `Fruitname`, FORMAT(AVG(`Rate`),2) FROM fruitsratelist  WHERE Fruitname=:Fruitname");
$STM->bindParam(':Fruitname', $Fruitname);
$STM->execute();
OR
Other method is to use unnamed parameters with question mark (?)
$STM = $dbh->prepare("SELECT  `Fruitname`, FORMAT(AVG(`Rate`),2) FROM fruitsratelist  WHERE Fruitname=?");
$STM->bindParam(1, $Fruitname);
$STM->execute();
OR
Below method is without binding and it can make code ripe for SQL Injections.
$STM = $dbh->prepare("SELECT  `Fruitname`, FORMAT(AVG(`Rate`),2) FROM fruitsratelist  WHERE Fruitname=$Fruitname1");
$STM->execute();

Tools Required

We used following tools and technologies for developing PHP MySQL Simple Select using PDO in Bootstrap Tutorial.
  • PHP as Server Side Scripting Language
  • MySQL as a Database.
  • Bootstrap as CSS Framework.
  • JQuery as a service.
  • phpMyAdmin for creating database and tables.
  • Sublime Text 2+ as a code editor.
  • XAMPP as a package for Apache web server, PHP and MySQL.
  • Google Chrome as a browser for testing.
Thanks for reading. Enjoy and share with friends.

Conclusion

We hope you will like PHP MySQL Simple Select using PDO in Bootstrap Tutorial and find it easy. We used latest PDO (PHP Data Objects) Prepared statements along with Bootstrap CSS Framework 2.3.2 for making this tutorial quickly. For any query/suggestions please send message on our Facebook Page.

No comments:

Post a Comment