php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37361 prepare statement alter the datatype of a parameter
Submitted: 2006-05-08 07:01 UTC Modified: 2006-05-09 12:32 UTC
From: axel dot azerty at laposte dot net Assigned:
Status: Not a bug Package: PDO related
PHP Version: 5.1.4 OS: Fedora Core 5 - Linux 2.6.16
Private report: No CVE-ID: None
 [2006-05-08 07:01 UTC] axel dot azerty at laposte dot net
Description:
------------
The prepared statement in PDO seems to lost or to have its type altered.

When typing a 'SELECT COALESCE(MAX(field),0) FROM table' under postgresql shell, no problem.
When using this query as is in PHP (with PDO), no problem.
When trying SELECT COALESCE(MAX(?),0) FROM table as a prepared statement, the execution fails.

Replacing "MAX(?),0" by "MAX(?),'0'" solves the problem, but the returned value is a string, and not an integer.



Reproduce code:
---------------
$stmt = $dbh->prepare("SELECT COALESCE(MAX(?),0) FROM board");
$stmt->bindParam(1,$fld);
$fld = "id_board";
if(!$stmt->execute()) print_r($stmt->errorInfo());

Expected result:
----------------
The expected result is "0" , in the case or the table is empty or the number of lines in the table.

Actual result:
--------------
The statement->errorInfo() returns : 
Array
(
    [0] => 42804
    [1] => 7
    [2] => ERREUR:  Les COALESCE types text et integer ne peuvent pas correspondre
)

In english : "the COALESCE types text and interger can't match".


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-05-08 13:05 UTC] smlerman at gmail dot com
bindParam and bindValue treat the parameter as a string by default, which means the value has special characters escaped and the entire value quoted. Your code produces COALESCE(MAX('id_board'),0), which is probably not what you want. You'll most likely need to place the field name directly in the query instead of trying to bind it as if it were normal data.
 [2006-05-09 12:21 UTC] wez@php.net
This is expected behaviour.

 [2006-05-09 12:32 UTC] axel dot azerty at laposte dot net
Could you explain me why ?
Shouldn't the datatype be the type of the field (or of the aggregate function like MAX()) ?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 07:01:27 2024 UTC