php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #49963 Need a function to obtain a list of PDO query parameters.
Submitted: 2009-10-23 02:01 UTC Modified: 2010-11-24 10:49 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: mikhail dot v dot gavrilov at gmail dot com Assigned:
Status: Open Package: PDO related
PHP Version: 5.3.0 OS:
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: mikhail dot v dot gavrilov at gmail dot com
New email:
PHP Version: OS:

 

 [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();


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-10-23 03:59 UTC] mikhail dot v dot gavrilov at gmail dot com
Here new function is getParameters().
This function return array with needed for bind parameters.
 [2010-11-24 10:49 UTC] jani@php.net
-Package: Feature/Change Request +Package: PDO related
 [2014-09-26 09:08 UTC] sebastianzartner at gmail dot com
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
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 31 23:01:28 2024 UTC