#40869 [NEW]: Mysql gives incorrect result, using subqueries and variables
| From: | robert dot allen at unearthtravel dot com | Date: | Tue, 20 Mar 2007 17:40:43 +0000 |
| Subject: | #40869 [NEW]: Mysql gives incorrect result, using subqueries and variables | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-110590@lists.php.net to get a copy of this message | ||
From: robert dot allen at unearthtravel dot com
Operating system:
PHP version: 5.2.1
PHP Bug Type: MySQL related
Bug description: Mysql gives incorrect result, using subqueries and variables
Description:
------------
Hi
We are getting an odd result when submitting a query to a mysql server
(4.1.10), we get a different result when performing the query through php
(an incorrect result) as opposed to straight in to a mysql client.
It seems to have performed the sub-query twice, so the variable @j has
been incremented twice.
A simplified version of our code is shown below and reproduces the
problem, I have also included a sample table. I have seen it on both
windows and linux.
Reproduce code:
---------------
mysql_query("SET @j=0;");
$sql = "select offset from (select @j:=@j+1 as offset,
if(element_id=2,1,0) as result from element) as t1 WHERE t1.result=1 ";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
echo $row[0];
/******************DATABASE****************/
CREATE TABLE
element (
element_id int(10) unsigned NOT NULL auto_increment,
element_type_id int(10) unsigned NOT NULL default '0',
lock_level int(10) unsigned NOT NULL default '0',
PRIMARY KEY (element_id),
KEY new_index (element_type_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*Data for the table element */
insert into
element(element_id,element_type_id,lock_level)
values
(1,1,0);
insert into
element(element_id,element_type_id,lock_level)
values
(2,10,0);
insert into
element(element_id,element_type_id,lock_level)
values
(3,2,0);
insert into
element(element_id,element_type_id,lock_level)
values
(4,10,1);
insert into
element(element_id,element_type_id,lock_level)
values
(5,2,0);
Expected result:
----------------
2
Actual result:
--------------
7
--
Edit bug report at http://bugs.php.net/?id=40869&edit=1
--
Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=40869&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=40869&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=40869&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=40869&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=40869&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=40869&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=40869&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=40869&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=40869&r=support
Expected behavior: http://bugs.php.net/fix.php?id=40869&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=40869&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=40869&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=40869&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=40869&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=40869&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=40869&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=40869&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=40869&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=40869&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=40869&r=mysqlcfg