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
