This code is licensed under Creative Commons 0 (Public Domain)
As of PHP 5.5, the MySQL functions are deprecated and are removed in PHP 7. The recommendation is to switch to MySQLi functions, which conveniently offer both a procedural (my preference) and an object-oriented structure. Luckily, it’s a rather easy transition to MySQLi as functions names and functions are nearly identical (just change mysql_ to mysqli_). The only exception to this is the mysql_result() function, which has no analog.
Quick review: mysql_result() is used to write less code when your database query is returning only a single row (LIMIT 1) and/or a single column.
$output = mysql_result($result,0); |
Pretty simple and straightforward. To replicate this in MySQLi, the following function can be used:
function mysqli_result($res,$row=0,$col=0){ $numrows = mysqli_num_rows($res); if ($numrows && $row <= ($numrows-1) && $row >=0){ mysqli_data_seek($res,$row); $resrow = (is_numeric($col)) ? mysqli_fetch_row($res) : mysqli_fetch_assoc($res); if (isset($resrow[$col])){ return $resrow[$col]; } } return false; } |
It has one improvement over mysql_result(), which is you can choose to only include the resource and no row and/or column. It will just assume mysqli_result($resource,0,0).
EDITED Dec 19, 2014
Function updated to allow for the column to be referenced by field name as well as numerical offset.
Thanks for this absolutely great function. Really got me out of trouble. :-)
Wow you are so amazing.
Perfect, that’s exactly what I was looking for. You saved my day, thanks!!
Thank you so much, it is very usefull function.
‘Xcellent merci ^_^
I am really very thankful for this help on mysqli_result() function
Very helpful function indeed, even in this age when we need to make an legacy PHP site run on a newer PHP version.
Pingback: Custom mysqli_result() function not plugging in well with my PHP script – GiveMeAns – Get the answers of your questions
I appreciate for this code, working perfectly fine. Thank you sir
BRILLIANT!… Thank you I’ve been going nuts try to do exactly the same thing as the old MySQL statement ..
Really has helped me out.. Thank you VERY MUCH!
Bravo thanks so much!
Thanks, It works fine in my login script..
It works like a charm! Thanks for developing and sharing this.
Currently migrating a large legacy app and this was a lifesaver, thanks!
Such a great code Mario, thank you!
Hi,
I am using:
for($i=0; $i < $qtde; $i++) {
$pet_id = mysql_result($dadosPets, $i, 'animal_id');
}
Now, how can I do the same but using mysqli.
Simple. Edit the example given above!
for($i = 0; $i < $qtde; ++$i)
$pet_id = mysqli_result($dadosPets, $i, 'animal_id');
Simple. Edit the example given above!
for($i = 0; $i < $qtde; ++$i)
$pet_id = mysqli_result($dadosPets, $i, 'animal_id');
Its Not Working
This is truly a great function. I’ve searched far and wide trying to find a way to return a single value without resorting to a while loop when I know all I need is one unique value using mysqli. Very elegant code. Thank you!
This is not working, if I call this function:
…
$book=mysqli_result($query,$i,”book”);
…
function mysqli_result($query,$row=0,$col=0)
{
if ($row>=0 && mysqli_num_rows($query)>$row)
{
mysqli_data_seek($query,$row);
$record = mysqli_fetch_row($query);
if (isset($record[$col]))
return $record[$col];
}
return false;
}