Fatal error: Uncaught Error: Call to undefined fun

2019-03-07 03:24发布

问题:

This question already has an answer here:

  • MySQLi equivalent of mysql_result()? 11 answers

I am getting this error while changing from mysql_result to mysqli_result

function f_exists($f_uname) {
     $f_uname = sanitize($f_uname);
     $conn = @mysqli_connect('localhost','root','','swift') or die($connect_error);
     $query = mysqli_query($conn,"SELECT COUNT(`f_id`) FROM `flight_users` WHERE `f_uname`= '$f_uname'") or die(mysqli_error($conn));


    //here is the problem
    return (mysql_result($query, 0) == 1) ? true : false; 

}

回答1:

Do not mix mysql_* and mysqli_*. Furthermore you cannot use mysql_result in the way you use it with mysql_*. Just replace this

return (mysql_result($query, 0) == 1) ? true : false; 

with the following:

if ($query && mysqli_num_rows($query) == 1) {  
    $row = mysqli_fetch_assoc()['count_val'];  
}

Therefore you need to use an alias for your count value in your statement, which you should always do: SELECT COUNT(f_id) as count_val ...

See this topic for more information about an equivalent to the mysql_result in mysql_*: MySQLi equivalent of mysql_result()?



标签: mysql result