PHP MySQL Simple Select using PDO in Bootstrap Tutorial

Furqan Aziz | 12 Jan 2014 | Tutorials

PHP MySQL Simple Select using PDO in Bootstrap Tutorial

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

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.

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.

Social Box

LATEST TUTORIALS
PHP MySQL Simple Delete using PDO in Bootstrap Tutorial
Convert Simple Text To 3D Text using Adobe Fireworks CS6 Tutorial
PHP Simple Update using PDO in Bootstrap Tutorial
PHP Simple Insert using PDO in Bootstrap Tutorial
PHP MySQL Easy CSV Data Insertion in Bootstrap using PDO Tutorial
PHP Easy Sign-in Using PDO Prepared Statement Tutorial
Easy PHP Email With Attachments using Swift Mailer Tutorial
Easy Pagination with PDO in PHP plus Export to Excel Tutorial
PHP PDO to Connect MySQL Database Tutorial
Best CSS Frameworks Collection For Rapid Web Development
PHP-MySQL Ajax Jquery-UI Autocomplete in Bootstrap Tutorial
PHP Simple Sign-in Tutorials
Jquery Raty Usage via PHP
PHP Dynamic Select options Via Ajax
CSV Importer in MySQL
PHP File Load Via Jquery into DIV
© 2014 CreWow Network - All Rights Reserved.