|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-10-23 02:01 UTC] 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();
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 11:00:02 2025 UTC |
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