php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60281 HAVING parameters cannot be bound in SQLite
Submitted: 2011-11-12 15:59 UTC Modified: 2011-11-12 16:49 UTC
From: nospam at unclassified dot de Assigned:
Status: Closed Package: PDO related
PHP Version: 5.3.8 OS: Windows, Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: nospam at unclassified dot de
New email:
PHP Version: OS:

 

 [2011-11-12 15:59 UTC] nospam at unclassified dot de
Description:
------------
An SQL query in PDO and SQLite which has a bound parameter in the having clause will return no result. If that parameter is inserted in the SQL string, the query works fine.

There is no URL to watch the bug, so here's my code:

Test script:
---------------
--- SQL setup:

create table things (name);

insert into things values ('car');
insert into things values ('car');
insert into things values ('car');
insert into things values ('car');
insert into things values ('car');
insert into things values ('car');
insert into things values ('bus');
insert into things values ('bus');
insert into things values ('bus');
insert into things values ('bus');
insert into things values ('bus');
insert into things values ('train');
insert into things values ('train');
insert into things values ('train');
insert into things values ('train');
insert into things values ('train');
insert into things values ('airplane');
insert into things values ('tree');
insert into things values ('tree');
insert into things values ('chair');
insert into things values ('chair');
insert into things values ('chair');

--- PHP code:

<?php
$sql = 'select name, count(*) cnt
	from things
	group by name
	having cnt >= ?
	order by cnt desc';
$params = array(3);

$pdo = new PDO('sqlite:havingtest.db');
$ps = $pdo->prepare($sql);
$r = $ps->execute($params);
$records = $ps->fetchAll(PDO::FETCH_ASSOC);

header('Content-type: text/plain');
var_dump($records);
?>


Expected result:
----------------
array(4) {
  [0]=>
  array(2) {
    ["name"]=>
    string(3) "car"
    ["cnt"]=>
    string(1) "6"
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(3) "bus"
    ["cnt"]=>
    string(1) "5"
  }
  [2]=>
  array(2) {
    ["name"]=>
    string(5) "train"
    ["cnt"]=>
    string(1) "5"
  }
  [3]=>
  array(2) {
    ["name"]=>
    string(5) "chair"
    ["cnt"]=>
    string(1) "3"
  }
}


Actual result:
--------------
array(0) {
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-11-12 16:49 UTC] nospam at unclassified dot de
Invalid bug. It's not about that parameters could not be bound, but SQLite seems particularly picky about the parameter type in a HAVING clause (as opposed to the WHERE clause), and PDO's stupid default PARAM_STR just doesn't fit here. You cannot use the execute($parameters) method here but need to bindParam(x, y, PDO::PARAM_INT/STR/...) every single parameter yourself. You might as well quote those values and put them into the SQL string, that's probably easier.
 [2011-11-12 16:49 UTC] nospam at unclassified dot de
-Status: Open +Status: Closed
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Wed Oct 27 04:03:38 2021 UTC