PHP Classes

Error in mysql2i function mysql_result

Recommend this page to a friend!

      PHP MySQL to MySQLi  >  All threads  >  Error in mysql2i function mysql_result  >  (Un) Subscribe thread alerts  
Subject:Error in mysql2i function mysql_result
Summary:mysql_result doesnt reset field pointer before looping
Messages:3
Author:mike mcdowell
Date:2017-08-09 16:15:44
 

  1. Error in mysql2i function mysql_result   Reply   Report abuse  
Picture of mike mcdowell mike mcdowell - 2017-08-09 16:15:44
Using your class mysql2i and results have been very good. We did discover what seems to be an in mysql_result. Our code often has multiple calls to mysql_result to get various fields. For example:

$super = mysql_result($R, 0, 'Super');
$client = mysql_result($R, 0, 'Client');
$book = mysql_result($R, 0, 'Book');

This may not be the most efficient way to get these fields but that is our code. Our problem was that the mysql_result replacement function does not reset the field pointer each time a new call is made. This means that the while loop simply picks up where it left off after the the previous call. Any fields examined trying to find the field name 'Super' from the example above will not be revisited when looking for the field name 'Client', etc. We added a call to mysqli_field_seek to reset the field pointer which seemed to fix the problem we were experiencing:

public static function mysql_result($result,$row,$field=null){

mysqli_data_seek($result,$row);
if( !empty($field) ){
# added next line to reset field pointer in $result
mysqli_field_seek($result,0);
while($finfo = mysqli_fetch_field($result)) {
if( $field == $finfo->name ){
$f = mysqli_fetch_assoc($result);
return $f[$field];
}
}
}

$f = mysqli_fetch_array($result);

return $f[0];

}

  2. Re: Error in mysql2i function mysql_result   Reply   Report abuse  
Picture of Dave Smith Dave Smith - 2017-09-04 04:13:22 - In reply to message 1 from mike mcdowell
Updated mysql_result to what is widely accepted as the best replacement. While the PHP manual does indicate that using mysqli_field_seek with mysqli_fetch_field is the correct approach, the replacement I am using actually eliminates the loop and fetches the row directly.

I am not 100% sure it will work for you, although everyone swears by it, so if you do update, be aware of the possible problem.

Dave

  3. Re: Error in mysql2i function mysql_result   Reply   Report abuse  
Picture of mike mcdowell mike mcdowell - 2018-01-11 22:31:33 - In reply to message 2 from Dave Smith
Dave--
Thanks for the warning about the update and potential problem. I tested your new code in version 1.5. It does work in my use case. Eliminating the while loop eliminated the need to reset the field pointer and the new code looks like a more general solution.

BTW, thanks for marking your changes so clearly in the updated class file. Makes it very easy to find areas that have changed in the code when upgrading.
Mike