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 && $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; }
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!