MySQLi Equivalent of mysql_result()
While transitioning PHP code from mysql to MySQLi, the absence of an equivalent to mysql_result() can pose a minor obstacle. This function allowed developers to retrieve a single value from a query result.
Solution:
Despite the lack of a direct equivalent, a custom function can replicate the functionality of mysql_result():
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; }
This function:
Example:
To use the function, simply replace the mysql_result() call with the following:
$blarg = mysqli_result($r, 0, 'blah');
Note:
This custom function excels when working with a single result and field, allowing for concise code. However, for larger datasets, it's still recommended to use the more efficient fetch_assoc() method.
The above is the detailed content of How to Replace mysql_result() with MySQLi in PHP?. For more information, please follow other related articles on the PHP Chinese website!