0

在mysql中,我有以下代码:

$row = mysql_fetch_assoc($query);
$dbactive = $row['active'];

if ($dbactive == 1){

...

}...

但我必须在我的项目中使用 mysqli。所以我尝试在下面更改它,但我不太正确。谁能帮我纠正mysqli中的以下语句,使其与上面的mysql语句相匹配?

更新:

我在 mysqli 中的尝试:

   // don't use $mysqli->prepare here
        $query = "SELECT TeacherForename, TeacherSurname, TeacherUsername, TeacherPassword, Active FROM Teacher WHERE TeacherUsername = ? AND TeacherPassword = ? LIMIT 1";
        // prepare query
        $stmt=$mysqli->prepare($query);
        // You only need to call bind_param once
        $stmt->bind_param("ss",$teacherusername,$teacherpassword);
        // execute query
        $stmt->execute(); 
        // get result and assign variables (prefix with db)
        $stmt->bind_result($dbTeacherForename,$dbTeacherSurname,$dbTeacherUsername,$dbTeacherPassword, $dbActive);

            while($stmt->fetch()) {

      if ($teacherusername == $dbTeacherUsername && $teacherpassword == $dbTeacherPassword) {
   if ($dbActive == 1){

        $loggedIn = true;
}
      }

}
4

4 回答 4

0

如果您只返回一行,则不需要while循环。bind_result()获取x列并将它们的值按顺序分配给您传入的变量。因此,假设您的结果仅包含一行一列,则与withbind_result($dbactive)相同。$dbactive = $row['active']mysql_*

于 2012-08-10T14:31:06.203 回答
0

绑定变量意味着无论何时调用 ->fetch(),结果行值都会自动放入该变量中。你正在用你的

$dbactive = $stmt['active'];

$stmt 是结果对象,而不是行对象或数组。它不会包含您通过查询提取的任何数据。

于 2012-08-10T14:31:59.690 回答
0

你可以只使用查询方法,

$result = $myslqi->query($query);
while($row = $result->fetch_object()){
  $row->active …
}
于 2012-08-10T14:33:39.067 回答
0

要获取单行,您可以使用:

if($query = $db->prepare("your sql query where field1=? AND field2=?")){

    $query->bind_param('ss', $user_id); 
    $query->execute();
    $query->bind_result($field1, $field2);
    $query->fetch();
    echo $field1;

}
于 2012-08-10T14:36:38.323 回答