mysqli_result() function to match mysql_result()

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.

20 thoughts on “mysqli_result() function to match mysql_result()

  1. Pingback: Custom mysqli_result() function not plugging in well with my PHP script – GiveMeAns – Get the answers of your questions

    1. Graeme Hird

      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!

      Reply
  2. Gustavo M Severo

    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.

    Reply
    1. Magictallguy

      Simple. Edit the example given above!
      for($i = 0; $i < $qtde; ++$i)
      $pet_id = mysqli_result($dadosPets, $i, 'animal_id');

      Reply
      1. Thangaraj

        Simple. Edit the example given above!
        for($i = 0; $i < $qtde; ++$i)
        $pet_id = mysqli_result($dadosPets, $i, 'animal_id');

        Its Not Working

        Reply
  3. Andrew Rowe

    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!

    Reply
  4. Gábor

    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;
    }

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *