#40869 [Opn->Fbk]: Mysql gives incorrect result, using subqueries and variables

From: Date: Tue, 20 Mar 2007 17:45:35 +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-110592@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:

Make sure PHP is linked with the correct libmysql, it should be of the
same version as the server.


Previous Comments:
------------------------------------------------------------------------

[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


Thread (12 messages)

« previous php.bugs (#110592) next »