Home > Backend Development > PHP Tutorial > How Can I Replace the mysql_result() Function When Migrating from MySQL to MySQLi?

How Can I Replace the mysql_result() Function When Migrating from MySQL to MySQLi?

Mary-Kate Olsen
Release: 2024-11-24 07:07:11
Original
684 people have browsed it

How Can I Replace the mysql_result() Function When Migrating from MySQL to MySQLi?

PHP Scripting: Migrating to MySQLi from MySql – Dealing with the Absence of mysql_result()

In transitioning old PHP code from MySQL to MySQLi, developers may encounter the absence of the mysql_result() function. This can pose a challenge, particularly when working with limited result sets.

The mysql_result() function, although inefficient for large datasets, offered simplicity when extracting a singular value from a single row and field. However, MySQLi lacks a direct equivalent function.

To overcome this inconvenience, a customized function has been developed to emulate the functionality of mysql_result() within the MySQLi environment. This function, mysqli_result(), replicates the behavior of its predecessor, returning false if the request is out of bounds. It accepts a row parameter that defaults to 0 and also allows for column specification by numerical offset or field name.

Here's the code for the mysqli_result() function:

function mysqli_result($res,$row=0,$col=0){ 
    $numrows = mysqli_num_rows($res); 
    if ($numrows &amp;&amp; $row <= ($numrows-1) &amp;&amp; $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;
}
Copy after login

Utilizing this function, developers can efficiently extract specific values from their MySQLi results, enhancing the portability of their code and simplifying their transition from MySQL.

The above is the detailed content of How Can I Replace the mysql_result() Function When Migrating from MySQL to MySQLi?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template