|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-08-29 15:18 UTC] chris dot vigelius at gmx dot net
Description:
------------
If a parameter appears more than once in a parametrized query, unexpected behaviour occurs
Reproduce code:
---------------
$dbh = new PDO(...)
$stmt = $dbh->prepare("SELECT * FROM user WHERE name=:user OR email=:user");
$stmt->execute(array(':user' => 'franz'));
Expected result:
----------------
only the rows are returned where either name or email are set to 'franz'
Actual result:
--------------
always all rows of the table are returned, which is clearly not what one would expect
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 11:00:01 2025 UTC |
// prepare the database $dbh = new PDO('mysql:host=localhost;dbname=test', 'test', 'test'); $dbh->exec("DROP TABLE IF EXISTS user"); $dbh->exec("CREATE TABLE user ( name VARCHAR(32), email VARCHAR(32), PRIMARY KEY (name) );"); $dbh->exec("INSERT INTO user VALUES ('fritz', 'fritz@server.com')"); $dbh->exec("INSERT INTO user VALUES ('otto', 'otto@server.com')"); // this will return 2 rows where 0 are expected $stmt = $dbh->prepare("SELECT * FROM user WHERE name=:user OR email=:user"); $stmt->execute(array(':user' => 'franz')); $rows = $stmt->fetchAll(PDO::FETCH_OBJ); var_dump($rows); // this workaround will return 0 rows as expected $stmt = $dbh->prepare("SELECT * FROM user WHERE name=:user1 OR email=:user2"); $stmt->execute(array(':user1' => 'franz', ':user2' => 'franz')); $rows = $stmt->fetchAll(PDO::FETCH_OBJ); var_dump($rows);Expected result: ---------------- array(0) { } array(0) { } Actual result: -------------- array(2) { [0]=> object(stdClass)#4 (2) { ["name"]=> string(5) "fritz" ["email"]=> string(16) "fritz@server.com" } [1]=> object(stdClass)#5 (2) { ["name"]=> string(4) "otto" ["email"]=> string(15) "otto@server.com" } } array(0) { }