PHPEveryday.com PHP and Web Development Tutorial
What are you looking for?


Table of Content
PHP Data Object Tutorial

PDO Intro
PDO Activation
PDO Connection
PDO Portable Conn
PDO Fetch Mode
PDO Error Hand.
PDO Statement
PDO Placeholders
PDO Insert & Update
PDO Bound Values
PDO BLOBs
PDO: Alt. BLOB
PDO Conn Attributes
PDO Error Mode
PDO Persistent
PDO Attributes
« PDO: Error Handling PDO: Positional and Named Placeholders »

PDO: Prepared Statement


PHP Data Objects (PDO) Step By Step Tutorial - Part 7: Do you ever hear about prepared statements? If you ever work with other tool such visual studio, may be you use prepare statement. PHP Extension for MySQL and SQLite don't offer this functionality. Ok, I will show a sample. I believe, from that sample you will understand what is prepare statement.
<?php
// configuration
$dbtype		= "sqlite";
$dbhost 	= "localhost";
$dbname		= "test";
$dbuser		= "root";
$dbpass		= "admin";

// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);

$title = 'PHP AJAX';

// query
$sql = "SELECT * FROM books WHERE title = ?";
$q = $conn->prepare($sql);
$q->execute(array($title));


$q->setFetchMode(PDO::FETCH_BOTH);

// fetch
while($r = $q->fetch()){
  print_r($r);
}


?>

in this simple example, query depends on a variabel (we write with ?).

$sql = "SELECT * FROM books WHERE title = ?";

Now, we manipulate this query to create the prepared statement and execute it:

$q = $conn->prepare($sql);
$q->execute(array($title))

Another sample:

$title = 'PHP%';
$author = 'Bobi%';
// query
$sql = "SELECT * FROM books WHERE title like ? AND author like ? ";
$q = $conn->prepare($sql);
$q->execute(array($title,$author));

Previous: PDO: Error Handling
Next: PDO: Positional and Named Placeholders



Series this article:
PDO: Introduction PHP Data Object
PDO: Activation PHP Data Objects Extension
PDO: Connecting Use PHP Data Object
PDO: Portable Connection to Database
PDO: Posibble Fetch Mode
PDO: Error Handling
PDO: Prepared Statement
PDO: Positional and Named Placeholders
PDO: Insert and Update Statement Use Prepared Statement
PDO: Prepared Statement and Bound Values
PDO: Working With BLOBs
PDO: Alternative Retrieve BLOB Data
PDO: Setting Connection Attributes
PDO: Error Mode Attributes
PDO: Improve Performance with Persistent Connection
PDO: Getting Connection Attributes


Tag: PHP Data Objects, PDO, extension, PHP extension, query, statement Category: PHP Database Post : April 19th 2008 Read: 6,409 Bookmark and Share

blog comments powered by Disqus


Database Tutorial
  • Learn PHP MySQL
  • Learn PHP ADOdb
  • Learn PHP Data Object/PDO
  • Learn PHP XML
  • Learn PHP SimpleXML
Security Tutorial
  • Learn PHP Security
  • Learn HTTP Authentication
  • Learn PHPSecureSite
Framework Tutorial
  • Learn CodeIgniter
  • Learn Joomla
  • Learn Smarty
  • Learn Zend Framework
Template Tutorial
  • Learn Joomla Template
  • Learn WordPress Template
API Tutorial
  • Learn Facebook
JS Framework Tutorial
  • Learn MooTools
  • Learn JQuery
AJAX Tutorial
  • Learn AJAX in 10 Minutes
  • Learn AJAX Client Side
  • Learn AJAX PHP
  • Learn AJAX Remote Server
  • Learn AJAX Repetitive
  • Learn AJAX MySQL
  • Learn AJAX Grid
Web Services Tutorial
  • Learn Web Services NuSOAP
  • Learn Web Services WSDL
  • Learn Web Services WSDL Array
  • Learn Web Services .NET Grid
  • Learn Web Services WDDX
Package Post
  • Joomla Intro
  • Joomla Component
  • Joomla Module
  • Joomla MVC
  • Joomla MVC Backend
  • PostNuke Intro
  • Zend Framework Intro
  • Zend Framework Action
  • Zend Framework Database
  • Zend Framework Registry
  • Zend Framework Config
  • Zend Framework Login
  • Zend Framework Session
  • PHP Array Tips
  • PHP File Tips
  • PHP Email
  • PHP Ms Excel
  • PHP Pattern
  • PHP SMS
  • Flash Database
  • PHP Multitier
  • jQuery Introduction
  • jQuery Selectors
  • Portable Web Server
  • Web Mobile Intro
  • Drupal Installation
  • Drupal Configuration