bind_param导致错误的原因

时间:2015-05-31 08:28:23

标签: php mysqli prepared-statement

$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if($conn->connect_error) {
  $this->last_error = 'Cannot connect to database. ' . $conn->connect_error;
}

mysqli_set_charset($conn, 'utf8');
// 2. Select a database to use 
$db_select = mysqli_select_db($conn, DB_NAME);
    if (!$db_select){
    die("Database selection failed: " . mysqli_error());
};

$query = $conn->prepare(
"INSERT INTO user (ID, Name, Gender, Email, Age, Text, Score) VALUES (NULL, ?, ?, ?, ?, ?, ?)"
);

$query->bind_param('sssssi', $name,$gender,$email,$age,$text,$score);
$query->execute();
$query->close();

我有这个错误:

  

在非对象

上调用成员函数bind_param()

但是当我切换到其他计算机并使用它时,我没有错误。

2 个答案:

答案 0 :(得分:0)

你应该这样做以捕获错误消息:

if(!($query = $conn->prepare("INSERT INTO user (ID, Name, Gender, Email, Age, Text, Score) VALUES (NULL, ?, ?, ?, ?, ?, ?)")))
    die($conn->error);

if(!$query->bind_param('sssssi', $name,$gender,$email,$age,$text,$score))
    die($conn->error);

$query->execute();
$query->close();

如果有任何进一步的帮助,请参阅输出。

答案 1 :(得分:0)

$query is probably `false` and since false is not an object, you are getting the error

var_dump($query);电话后立即使用prepare并检查您是否收到任何类型的邮件。

ALSO ,如果sql查询错误构造并发送到服务器,prepare()将返回false

注意:这是Documentation所说的

  

mysqli_prepare()返回一个语句对象,如果有错误则返回FALSE   发生。