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

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度