#40869 [Opn->Fbk]: Mysql gives incorrect result, using subqueries and variables
| From: | tony2001@php.net | Date: | Tue, 20 Mar 2007 18:12:10 +0000 |
| Subject: | #40869 [Opn->Fbk]: Mysql gives incorrect result, using subqueries and variables | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-110594@lists.php.net to get a copy of this message | ||
ID: 40869
Updated by: tony2001@php.net
Reported By: robert dot allen at unearthtravel dot com
-Status: Open
+Status: Feedback
Bug Type: MySQL related
PHP Version: 5.2.1
New Comment:
Which OS is that?
Previous Comments:
------------------------------------------------------------------------
[2007-03-20 18:07:52] robert dot allen at unearthtravel dot com
To be honest I am not sure how to do this, I have tried it on an xampp
bundle though and so presume they would have linked them correctly. I
have also tried on our hosted managed server and get the same result in
both places.
Thanks for your help
Rob
------------------------------------------------------------------------
[2007-03-20 17:45:35] tony2001@php.net
Make sure PHP is linked with the correct libmysql, it should be of the
same version as the server.
------------------------------------------------------------------------
[2007-03-20 17:40:43] robert dot allen at unearthtravel dot com
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 this bug report at http://bugs.php.net/?id=40869&edit=1