phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


PDO: Prepared Statement and Bound Values

Tag: PHP Data Objects, PDO, extension, PHP extension, query, statement, prepared statement, bound values   Category: PHP Database
post: 19 Apr 2008 read: 523


PHP Data Objects (PDO) Step By Step Tutorial - Part 10: PDO also supports bound statements where you can explicitly bind an immediate value or a variable to a named or positional placeholder. Look this sample:

<?php
// configuration
$dbtype		= "sqlite";
$dbhost 	= "localhost";
$dbname		= "test";
$dbuser		= "root";
$dbpass		= "admin";

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

// new data

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

$q->bindColumn(1, $id);
$q->bindColumn(2, $title);
$q->bindColumn(3, $author);

while($q->fetch())
{
echo "$title, $author <br/>";
}

?>

For insert data:


// query
$sql = "INSERT INTO books (title,author) values(?,?)";

$q = $conn->prepare($sql);
$q->bindColumn(1, $title);
$q->bindColumn(2, $title);

$q->execute();



| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


615
posting