php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22328 Erroneous behaviour of fetchRow method
Submitted: 2003-02-20 05:23 UTC Modified: 2003-06-24 07:29 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: miki_fossati at libero dot it Assigned: georg (profile)
Status: Not a bug Package: MySQL related
PHP Version: 4.3.3RC2-dev OS: linux suse 7.2
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: miki_fossati at libero dot it
New email:
PHP Version: OS:

 

 [2003-02-20 05:23 UTC] miki_fossati at libero dot it
In the following code:

--- starts here ---
require_once 'DB.php';
$connString = "mysql://user:pass@localhost/my_db";
$mysqlDb = DB::connect("$connString", true);

$result = $mysqlDb->query("SELECT id FROM table WHERE name='foo' LIMIT 0, 3");
if (!DB::isError($result))
{
	while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
		extract($row);
		echo "$id <br />";
		$result2=$mysqlDb->query("FAKE QUERY");
		if(!DB::isError($result2))
		{
//Never here
		}
                else {
//Continue loop
                }
	}
}
--- ends here ---

the loop continues forever.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-04-28 03:58 UTC] arnaud@php.net
changing status
 [2003-04-29 15:14 UTC] lsmith@php.net
the problem is in the implementation of fetchInto:

    function fetchInto($result, &$arr, $fetchmode, $rownum=null)
    {
        if ($rownum !== null) {
            if (!@mysql_data_seek($result, $rownum)) {
                return null;
            }
        }
        if ($fetchmode & DB_FETCHMODE_ASSOC) {
            $arr = @mysql_fetch_array($result, MYSQL_ASSOC);
        } else {
            $arr = @mysql_fetch_row($result);
        }
        if (!$arr) {
            $errno = @mysql_errno($this->connection);
            if (!$errno) {
                return NULL;
            }
            return $this->mysqlRaiseError($errno);
        }
        return DB_OK;
    }

this line picks up the error that was meant for the "FAKE QUERY" and think by mistake that therefore not the end of the result set was reached but an error occured.

Since during a fetch only a connection error can occur it might be a good idea to remove the mysql_errno() call in order to fix this.

Alternative we could check for a connection error:
        if (!$arr) {
            $errno = @mysql_errno($this->connection);
            if ($errno == 2013) {
                return $this->mysqlRaiseError($errno);
            }
            return NULL;
        }
$errno = @mysql_errno($this->connection);
 [2003-05-11 01:19 UTC] mj@php.net
Actually lsmith's fix seems to be fine. Is there anything speaking against applying it?
 [2003-06-21 11:40 UTC] cox@php.net
Ok, I've removed the mysql_errno() check on fetchInto().

IMHO this is a bug in the mysql extension, which doesn't properly reset the error. This problem doesn't ocurr with postgres or interbase.

Test script to reproduce the problem:
<?php
$con = mysql_connect('localhost', 'root');
mysql_selectdb('test');
$res = mysql_query('SELECT * FROM table');
$continue = true;
do {
    $row = mysql_fetch_row($res);
    if (mysql_errno()) {
        // Here there is no fetch_row error, it's the same
        // from FAKE QUERY
        echo "Failed fetching with error: " . mysql_errno() . "\n";
        // Don't want to continue on fetch errors
        $continue = false;
    } elseif (!$row) {
        $continue = false;
    } else {
        $res2 = mysql_query('FAKE QUERY');
        echo "Failed to launch FAKE QUERY: " . mysql_errno() . "\n";
        // Ok, I got the error but want to continue
        $continue = true;
    }
} while ($continue);

?>

Output:
Failed to launch FAKE QUERY: 1064
Failed fetching with error: 1064


 [2003-06-23 19:30 UTC] sniper@php.net
Happens with PHP 4.3.3RC2-dev, Georg? :)

 [2003-06-24 07:29 UTC] georg@php.net
It's a bug in libmysql which is already reported (mysql bug #
706)
 [2004-06-22 05:18 UTC] tko at lostway dot org
"mysql bug #706" is not a bug. It is a specification.

So you should check mysql_errno().

A client user should reset errno somehow (I think of re-connecting db) if he needs.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 05:01:30 2024 UTC