dongqian5569 2014-10-13 22:09
浏览 360
已采纳

SELECT返回mysqli_num_rows给出错误:类mysqli_result的对象无法转换为字符串

I have the following function. It's aim is to check the database users and see if the email is present in the email column.

If it is present, it should give me a result of int 1 using mysqli_num_rows which I'll use to show the user an error, (sorry, your email is already registered).

If it isn't present, then mysqli_num_rows should return int 0 and the code can continue.

My problem is with this assigning of the SELECT result.

function checkEmail($email,$name){
    // Connects to DB
    $con = connectDB();

    // Creates the SQL to be run
    $sql = "SELECT email FROM `users` WHERE email = '" . $email . "'";

    // Runs the query
    $emailExists = mysqli_query($con,$sql);

    // Uses mysqli_num_rows to return the number of rows with the query
    $num_rows = mysqli_num_rows($emailExists);
    var_dump($num_rows);

    // if no email exists in column
    if ($num_rows == 0){
        // return this to be used elsewhere
        return $emailExists;
    }else{
        // send to error page and halt script
        Header('Location:error.php?error=emailexists&name=' . $name);
        return false;
    }
    mysqli_close($con);
}

But when I run this far, I get this error:

Object of class mysqli_result could not be converted to string
  • 写回答

1条回答 默认 最新

  • dougupang0901 2014-10-13 22:16
    关注

    $emailExists is a mysqli_result object

    When you use it elsewhere, you are expecting it to be a string. Try doing

    return mysqli_fetch_array($emailExists); 
    

    instead of

    return $emailExists;
    

    This will return an array, so to access the record you want to use $returnedvalue[0]['column_name'] to access your data

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化