我想获取此行并将其保存到$notescheck,但是当我尝试执行此操作时,当我想回显并且没有错误时,$notescheck为空。对于未准备好的语句,它可以正常工作。

码:

if($user_ok == true) {
    $sql = "SELECT notescheck FROM users WHERE username=? LIMIT 1";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("s",$log_username);
    $stmt->execute();
    $row = $stmt->fetch();
    $notescheck = $row[0];
    $stmt->close();
}


使用未准备好的语句,它将看起来像这样:

 if($user_ok == true) {
    $sql = "SELECT notescheck FROM users WHERE username='$log_username' LIMIT 1";
    $query = mysqli_query($conn, $sql);
    $row = mysqli_fetch_row($query);
    $notescheck = $row[0];
    mysqli_close($conn);
}

最佳答案

这不是fetch()与准备好的语句一起工作的方式,不是像您认为的那样获取数组。您还需要将选择的结果绑定到变量中,然后使用它们进行显示。如果有多个记录,则应使用while($stmt->fetch){ echo $notescheck };

if($user_ok == true) {
    $sql = "SELECT notescheck FROM users WHERE username=? LIMIT 1";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("s",$log_username);
    $stmt->execute();
    $stmt->bind_result($notescheck);
    $stmt->fetch();
    $stmt->close();
}
echo $notescheck;


您应该阅读以下内容:

http://php.net/manual/en/mysqli-stmt.fetch.php

多个与username = x匹配的记录如下所示:

if($user_ok == true) {
        $sql = "SELECT notescheck FROM users WHERE username=? LIMIT 1";
        $stmt = $conn->prepare($sql);
        $stmt->bind_param("s",$log_username);
        $stmt->execute();
        $stmt->bind_result($notescheck);
        $stmt->store_result()
        while($stmt->fetch()){
           echo $notescheck;
        }
        $stmt->close();
    }

关于php - 准备好的语句获取行不返回任何内容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46225462/

10-09 05:43