Mysqli准备声明不起作用

时间:2014-04-10 12:57:11

标签: php

尝试使用MySQLi执行预准备语句时出现以下错误。

任何帮助确定我做错误的做法都会非常感激。

  

致命错误:在etc。中的非对象上调用成员函数prepare()

我使用此声明正确连接:

<?php
$DBHost = "localhost";
$DBUser = "xxxxxx";
$DBPass = "xxxxxx";
$DBName = "creati38_chicos";
$dbconn = new mysqli($DBHost, $DBUser, $DBPass, $DBName);
// check connection
if ($dbconn->connect_error) {
//echo $dbconn->connect_error;
die('Sorry, we are having some problems right now. Please check back later.'); 
}
else{
echo "You have connected Successfully";
}
?>

我正在使用这句话:

<?php
if ((isset($_POST["update"])) && ($_POST["update"] == "Update Appliance")) {

$stmt = $mysqli->prepare("UPDATE appliances SET
    category = ?,
    make = ?,
    model = ?, 
    description = ?, 
    price = ? 
    WHERE id = ?");
$stmt->bind_param('ssssdi',
    $_POST['category'],
    $_POST['make'],
    $_POST['model'],
    $_POST['description'],
    $_POST['price'],
    $_POST['id']);
$stmt->execute();
$stmt->close();

}
 ?>

3 个答案:

答案 0 :(得分:0)

你在做什么

$dbconn = new mysqli($DBHost, $DBUser, $DBPass, $DBName);

并使用

$mysqli->prepare()

使两者相同

答案 1 :(得分:0)

使用 $ dbconn 代替 $ mysqli     

$stmt = $dbconn->prepare("UPDATE appliances SET
    category = ?,
    make = ?,
    model = ?, 
    description = ?, 
    price = ? 
    WHERE id = ?");
$stmt->bind_param('ssssdi',
    $_POST['category'],
    $_POST['make'],
    $_POST['model'],
    $_POST['description'],
    $_POST['price'],
    $_POST['id']);
$stmt->execute();
$stmt->close();

}
 ?>

答案 2 :(得分:0)

在您的情况下,它不是$mysqli->prepare(,而是$dbconn->prepare(