【发布时间】:2020-06-12 08:59:09
【问题描述】:
我最近在 youtube 上关注了 mmtuts 关于 PHP OOP 的教程播放列表,但他使用了 PDO,而我没有,当我尝试在我的项目中实现代码时出现错误。
我的代码:
//In file with class 'Article'.
public function deleteArticle($id) {
$conn = $this->connect();
$sql = "UPDATE article SET deleted=1 WHERE row_id=?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
return $result;
}
//In file with class 'DBConn'.
public function connect() {
$conn = new mysqli('localhost', 'root', '','nicms');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
return $conn;
}
//In file with class 'articleContr'.
public function showDeleteArticle($id) {
if ($this->deleteArticle($id)
echo "Article has successfully been deleted.";
else
echo "Failed to delete article.";
}
错误是文章确实被删除了,但方法 showDeleteArticle 给出了消息“无法删除文章”。即使文章已被删除。
【问题讨论】:
-
PHP 文档说 get_result 只为 SELECT 查询返回值,而为所有其他查询类型返回 false。您是否阅读过文档以了解在执行 INSERT/UPDATE/DELETE 查询时应该做什么?php.net/manual/en/mysqli-stmt.get-result.php