Req #70128 [NEW]: PDO: Support named and question mark placeholders (parameters) in single query

From: Date: Fri, 24 Jul 2015 14:18:41 +0000
Subject: Req #70128 [NEW]: PDO: Support named and question mark placeholders (parameters) in single query
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-194680@lists.php.net to get a copy of this message
From:             chealer at gmail dot com
Operating system: 
PHP version:      Irrelevant
Package:          PDO related
Bug Type:         Feature/Change Request
Bug description:PDO: Support named and question mark placeholders (parameters) in single query

Description:
------------
As explained in http://php.net/manual/en/pdo.prepare.php,
"You cannot
use both named and question mark parameter markers within the same SQL
statement; pick one or the other parameter style."

Indeed, this can become a "style" issue for complex queries built by
several functions, somewhat forcing applications to choose one or the
other.

I believe named parameter markers are generally better, providing better
clarity, but they are sometimes inferior to question mark parameter
markers. Consider the broken test script (2 columns are named "name").
The filterType() function here could easily make itself clever enough to
workaround keeping only named markers, but it would be simpler to use a
mix of both Oracle-style (named) and MySQL-style (question mark)
parameters:

function filterType($name, &$parameters) {
    $parameters[] = $name;
    return '(type=(SELECT id FROM types WHERE name= ?))';
}


Question mark parameter markers have their issues (order matters), and
named markers have their different issues (unicity requirement). The
more complex query generation gets, the more complex using only one
parameter style becomes. Therefore, I believe both styles should be
allowed in the same query.


Until this is implemented, I suggest putting the quote from the manual
above in a warning box.

Test script:
---------------
<?php
function filterType($name, &$parameters) {
    $parameters[':name'] = $name;
    return '(type=(SELECT id FROM types WHERE name= :name))';
}

$parameters = array(':name' => 'salsa');
$sql = 'SELECT colour, calories
    FROM fruit
    WHERE name = :name';
$sql .= ' AND ' . filterType('sauce', $parameters); // Make sure we
don't select something called "salsa" which is not sauce.
$sth = $dbh->prepare($sql);
$sth->execute($parameters);
$sth->fetchAll();
?>


-- 
Edit bug report at https://bugs.php.net/bug.php?id=70128&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=70128&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=70128&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=70128&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=70128&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=70128&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=70128&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=70128&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=70128&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=70128&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=70128&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=70128&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=70128&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=70128&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=70128&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=70128&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=70128&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=70128&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=70128&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=70128&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=70128&r=mysqlcfg



Thread (2 messages)

« previous php.bugs (#194680) next »