我试图创建一个简单的api,为表添加视图。然后,我尝试使用预准备语句以避免SQL注入,但似乎无法使其工作。它会在以下错误后继续返回:(Fatal error: Call to a member function bind_param() on a non-object in)
$con = new mysqli('host','user','pass','db');
$type = $_GET['type'];
$identifier = $_GET['identifier'];
$news = $_GET['newsid'];
$check = $con->prepare("SELECT * FROM news WHERE news.news_id =? OR news.type =? OR news.identifier=?");
$check->bind_param("iss", $news, $type, $identifier);
$check->execute();
if ($check->fetchColumn() > 0) {
$add_view = $con->prepare("INSERT INTO views VALUES (:news_id, :identifier, :type, CURRENT_TIMESTAMP())");
$add_view->bindValue(':news_id', $news);
$add_view->bindValue(':identifier', $identifier);
$add_view->bindValue(':ntype', $type);
$add_view->execute();
}
答案 0 :(得分:2)
我认为您在这里混合mysqli
和PDO
实施。您应该bind_param
使用mysqli
。 bindParam
和bindValue
是PDO
s。
答案 1 :(得分:0)
开启警告。您的语法中某处出现错误。因此$con->prepare
会返回false
并发出警告。
您可以在$con->error
。