致命错误:在布尔值上调用成员函数bind_param()

时间:2017-04-14 03:40:41

标签: php mysql mysqli

我正在创建一个从两个表中获取数据的登录脚本。我明白当语句返回FALSE AKA一个布尔值时会发生这个错误,但为什么它返回false? 我做了一个功能,直到某个点

db.getCollection('booking').aggregate([
    {
        $match: {
            property: "001",
            checkin: {$gte: 1483596800},
            checkout: {$lte: 1583596800}
        }
    },
    {
        $lookup: {
            from: "users",
            localField: "user",
            foreignField: "_id",    
            as: "users"     
        }   
    },
    { $unwind: "$users" },
    {
        $lookup: {
            from: "users",
            localField: "agent",
            foreignField: "_id",    
            as: "agent"     
        }   
    },
    { $unwind: "$agent"} 
])



booking Table
{
    "_id" : "AAAAA",
    "property" : "001",
    "user" : "U001",
    "agent" : "A001",
    "checkin" : 1493596800,
    "checkout" : 1494374400,
    "test" : "This one will always work"
}
{
    "_id" : "BBBBB",
    "property" : "001",
    "user" : "U001",
    "agent" : "",
    "checkin" : 1493596800,
    "checkout" : 1494374400,
    "test" : "This one has blank agent and does not work"
}
{
    "_id" : "CCCCC",
    "property" : "001",
    "user" : "U001",
    "checkin" : 1493596800,
    "checkout" : 1494374400,
    "test" : "This one has no agent and does not work"
}
{
    "_id" : "DDDDD",
    "property" : "001",
    "user" : "U001",
    "agent" : "XXXX",
    "checkin" : 1493596800,
    "checkout" : 1494374400,
    "test" : "This one has invalid agent and does not work"
}


users Table 
{
    "_id" : "U001",
    "name" : "I am USER"
}
{
    "_id" : "A001",
    "name" : "I am AGENT"
}

它很有效,直到我尝试从表中获取更多列,甚至尝试从表中 function loginall($username, $password) { $db_host="localhost"; $db_username="root"; $db_password=""; $db_name="name"; $con=mysqli_connect($db_host, $db_username,$db_password, $db_name); $mysqli = new mysqli("$db_host","$db_username","$db_password", "$db_name"); $qry = "SELECT username, password, level, active FROM businesses WHERE username=? AND password=? UNION SELECT username, password, level, active FROM employees WHERE username=? AND password=?"; $stmt = $mysqli->prepare($qry); $stmt->bind_param("ssss", $u,$p,$uu,$pp); $u = $username; $p = $password; $uu = $username; $pp = $password; $stmt->execute(); $result = $stmt->get_result(); while($row = $result->fetch_array(MYSQLI_ASSOC)) { return $row; } } 。我通读了其他类似的问题并找到了代码来显示错误,但没有运气。谢谢!

2 个答案:

答案 0 :(得分:0)

你的函数一旦到达循环中的第一个return语句就会结束/返回(第一次迭代)。 您需要构建完整的数组,然后返回一次。

应该这样做:

if(!($stmt=$mysqli->prepare($qry))){
    return ["Prepare failed: ".mysqli_error($mysqli)];  // what does this say?
}elseif(!$stmt->bind_param("ssss",$u,$p,$uu,$pp)){
    return ["Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error];
}else{
    $u = $username;
    $p = $password;
    $uu = $username;
    $pp = $password;
    if (!$stmt->execute()){
        return ["Execute failed: (" . $stmt->errno . ") " . $stmt->error];
    }else{
        $result = $stmt->get_result();
        while($row = $result->fetch_array(MYSQLI_ASSOC)){
            $rows[]=$row;
        }
        return $rows;
    }
}

尝试回填所有列名称。 LEVEL是MySQL KEYWORD。

答案 1 :(得分:-1)

尝试这可能是bind_result()而不是get_result()

您可能想知道为什么甚至使用bind_result()

这完全取决于首选,因为语法被认为更具可读性。

但是,应该注意bind_result()不能与*通配符选择器一起使用。它必须包含显式值

在此代码中使用bind_result(),值$usernameRow, $passwordRow, ....构成了数据库tebles:

.....
...
.
$stmt->bind_param("ssss", $username, $password, $username, $password);
$stmt->execute();
$stmt->store_result();
$numRows = $stmt->num_rows;
$stmt->bind_result($usernameRow, $passwordRow, $levelRow, $activeRow); 
if($numRows > 0) {
  while ($stmt->fetch()) {
    $u[] = $usernameRow;
    $p[] = $passwordRow;
    $uu[] = $levelRow;
    $pp[] = $activeRow;
  }
}
$stmt->close();