phpeveryday.com

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


PDO: Positional and Named Placeholders

Tag: PHP Data Objects, PDO, extension, PHP extension, query, statement, positional placeholders, named placeholders   Category: PHP Database
post: 19 Apr 2008 read: 595


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.




| 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
SMS : Sending SMS with PHP and ActiveXperts (Part 1)

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


624
posting