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

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!