PDO: Positional and Named Placeholders


PHP Data Objects (PDO) Step By Step Tutorial - Part 8: We had known about prepare statement from previous post. Now, we talk more deep about placeholder.

Positional Placeholders

Give attention to this query:

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

The query above used question marks to designate the position of values in the prepared statement. These question marks are called positional placeholders. We must take care of proper order of the elements in the array that we are passing to the PDOStatement::execute() method.

Named Placeholders

Give attention to this query:

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

This use descriptive names preceded by a colon, instead of question marks. We don't care about position/order of value. That's why it called named placeholders.

Previous: PDO: Prepared Statement
Next: PDO: Insert and Update Statement Use Prepared Statement



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

Bookmark and Share Tag: PHP Data Objects, PDO, extension, PHP extension, query, statement, positional placeholders, named placeholders Category: PHP Database Post : April 19th 2008 Read: 73,381

advertisements


blog comments powered by Disqus