在MySQLi中的select语句中获取绑定参数错误 [英] Getting error on binding param in select statement in MySQLi

查看:50
本文介绍了在MySQLi中的select语句中获取绑定参数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面添加了一个精选代码段.为什么在bind_param()上出现以下错误?

I have added a select snippet below. Why am I getting the following error on bind_param()?

未捕获的错误:在布尔值上调用成员函数bind_param()

Uncaught Error: Call to a member function bind_param() on boolean

代码:

$sessien = $_POST['xsession'];
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
$query = "SELECT `post` FROM `user` WHERE session=? ORDER BY `thedate` DESC ";
$stmt = $conn->prepare($query);
$stmt->bind_param("s", $sessien);
$stmt->execute();
while ($stmt -> fetch()) {
        echo "$post<br>";
}
$stmt->close();
$conn->close();

推荐答案

在绑定之前,Mysqli prepare可以返回false,您必须检查它是否存在错误. 看看php.net中的这篇文章

Mysqli prepare can returns false before bind you must check it for errors. look at this article in php.net

http://php.net/manual/en/mysqli.error.php

    $sessien = $_POST['xsession'];
    $conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
    /* check connection */
    if ($conn->connect_errno) {
        printf("Connect failed: %s\n", $conn->connect_error);
        exit();
    }
    $query = "SELECT `post` FROM `user` WHERE session=? ORDER BY `thedate` DESC ";
    $stmt = $conn->prepare($query);
    if ($stmt) {
        $stmt->bind_param("s", $sessien);

        //bind Response variables
        $stmt->bind_result($post);
        $stmt->execute();
        while ($stmt -> fetch()) {
                echo "$post<br>";
        }
        $stmt->close();
    }else{
           //error
           var_dump($conn->error);
    }

这篇关于在MySQLi中的select语句中获取绑定参数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆