<?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