October 12, 2015

mysqli_result() Function PHP5.5+ & PHP7

REFERENCED FROM: http://mariolurig.com/coding/mysqli_result-function-to-match-mysql_result/

In PHP 5.5 and above the MySQL functions are deprecated and have been removed PHP7. The recommendation is to switch to MySQLi or use the PDO MySQL extension. The following code will replicate the mysql_result code from previous versions of PHP.

“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.”

Old code, PHP 5.4 and below:

$output = mysql_result($result,0);

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).”

Leave a Reply

Your email address will not be published.

css.php