PDO: Prepared Statement and Bound Values



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();

Previous: PDO: Insert and Update Statement Use Prepared Statement
Next: PDO: Working With BLOBs



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, prepared statement, bound values Category: PHP Database Post : April 19th 2008 Read: 4,350 Bookmark and Share

blog comments powered by Disqus