【问题标题】:Prepared statement fetch row returns nothing准备好的语句获取行不返回任何内容
【发布时间】:2017-09-15 01:52:37
【问题描述】:

我想获取这一行并将其保存到$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);
}

【问题讨论】:

    标签: php mysql prepared-statement fetch


    【解决方案1】:

    这不是 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();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-02
      • 2015-07-13
      相关资源
      最近更新 更多