Req #49963 [Com]: Need a function to obtain a list of PDO query parameters.
| From: | sebastianzartner at gmail dot com | Date: | Fri, 26 Sep 2014 09:08:52 +0000 |
| Subject: | Req #49963 [Com]: Need a function to obtain a list of PDO query parameters. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-187714@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=49963&edit=1
ID: 49963
Comment by: sebastianzartner at gmail dot com
Reported by: mikhail dot v dot gavrilov at gmail dot com
Summary: Need a function to obtain a list of PDO query
parameters.
Status: Open
Type: Feature/Change Request
Package: PDO related
PHP Version: 5.3.0
Block user comment: N
Private report: N
New Comment:
I see two ways to solve this.
1. Function returning a key/value array:
public array PDOStatement::getParameters ( void )
Where the array keys would be the parameter names and the array values the parameter values.
I guess this is what Mikhail suggests.
2. Introduce a PDOParameter class:
The class would look like this:
PDOParameter {
/* Properties */
mixed $key;
int $dataType;
int $length;
mixed $value;
/* Methods */
public void bind ( mixed &$value [, int $data_type = PDO::PARAM_STR [, int $length [, mixed
$driver_options ]]] )
}
Where $key is a string for named parameters and an integer for unnamed parameters, $dataType is one
of the PDO::PARAM_* constants, $length is the length required for OUT parameters and $value is the
value assigned to the parameter. bind() would work like PDOStatement::bindParam().
Having this additional class, a new parameter $params could be added to the PDOStatement class,
which would be an array of PDOParameters - indexed in case of unnamed params, associative in case of
named params.
Usage example with named parameters:
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour = :colour');
$sth->params[':calories']->bind($calories, PDO::PARAM_INT);
$sth->params[':colour']->bind(12, PDO::PARAM_STR);
Usage example with unnamed parameters:
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < ? AND colour = ?');
$sth->params[0]->bind($calories, PDO::PARAM_INT);
$sth->params[1]->bind(12, PDO::PARAM_STR);
This syntax has the advantage that you can even manipulate the parameters.
As the latter includes a lot of new logic, I could also create an RFC[1] if wished.
Sebastian
[1] https://wiki.php.net/rfc
Previous Comments:
------------------------------------------------------------------------
[2009-10-23 03:59:24] mikhail dot v dot gavrilov at gmail dot com
Here new function is getParameters().
This function return array with needed for bind parameters.
------------------------------------------------------------------------
[2009-10-23 02:01:45] mikhail dot v dot gavrilov at gmail dot com
Description:
------------
Feature request.
Need a function to obtain a list of PDO query parameters. To be able to universal queries without
the need to create an individual code for each request with bindValue functions.
Example:
$sth = $GLOBALS['db']['conn']->prepare($query);
$bindpars = $sth->getParameters();
foreach ($bindpars as $idx => $value)
if(isset($_POST[substr($idx, 1)])) $sth->bindValue($idx, $_POST[substr($idx, 1)]); else
$sth->bindValue($idx, '');
$sth->execute();
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=49963&edit=1