sql 仅适用于 mysql 但不适用于 php
sql works only in mysql but not in php
我 运行 陷入了一个相当奇怪的错误。
我在 mysql 中编写了我的 sql 查询,以确保它们在我实施之前有效。
这个查询工作正常:
SELECT articles.article_id,
articles.previewtext,
articles.date,
articles.title,
articles.previewtext,
image.thumbnail,
articleAuthTable.nickname AS authorNickname,
imageAuthTable.nickname AS imageNickname
FROM articles
LEFT JOIN image
ON articles.previewimage = image.image_id
LEFT JOIN authors AS articleAuthTable
ON articles.author = articleAuthTable.author_id
LEFT JOIN authors AS imageAuthTable
ON image.author = imageAuthTable.author_id
WHERE articles.categories = 'gamedev'
AND articles.article_id > 0
ORDER BY date DESC;
所以我在我的 php class 中实现了它(一如既往)并试图获得结果。它们是空的,结果是 "you have an error in you mysql"。
一如既往,我尝试将查询作为字符串回显并在 mysql 中重新运行它。奇迹般有效。在 php 中没有。同样的错误。
我试图将 sql 查询分成几部分,以查找错误,该错误似乎只存在于语句中:
[...] articles.article_id > 0
这会触发我的 php sql 查询崩溃。它在 php 中不会以任何方式工作。但在 mysql 中,它运行完美,我得到了正确的数据。 php 中的任何其他 SQL 查询都可以完美运行。
不起作用的部分应该限制结果。
编辑:
PHP代码
public function queryWhile($query){
$query = htmlspecialchars($query);
$this->result = mysqli_query($this->db, $query);
if(!$this->result){
echo "No response";
$this->closeDB();
}else{
while ($row = mysqli_fetch_array($this->result)){
$this->resultArray[] = $row;
}
}
}
return $this->queryWhile("SELECT articles.article_id, articles.PreviewText, articles.date,articles.Title, articles.previewText, image.thumbnail, articleAuthTable.nickname AS authorNickname, imageAuthTable.nickname AS imageNickname FROM articles LEFT JOIN image ON articles.previewImage = image.image_id LEFT JOIN authors AS articleAuthTable ON articles.author = articleAuthTable.author_id LEFT JOIN authors AS imageAuthTable ON image.author = imageAuthTable.author_id WHERE articles.categories = '".$this->category."' ORDER BY date DESC;");
$query = htmlspecialchars($query);
正在转化
articles.article_id > 0
到
articles.article_id > 0
MySQL 显然不知道该做什么。在 SQL 语句中使用 htmlspecialchars
真的没有什么好的理由,除非您打算将其打印在网页上。
我 运行 陷入了一个相当奇怪的错误。
我在 mysql 中编写了我的 sql 查询,以确保它们在我实施之前有效。
这个查询工作正常:
SELECT articles.article_id,
articles.previewtext,
articles.date,
articles.title,
articles.previewtext,
image.thumbnail,
articleAuthTable.nickname AS authorNickname,
imageAuthTable.nickname AS imageNickname
FROM articles
LEFT JOIN image
ON articles.previewimage = image.image_id
LEFT JOIN authors AS articleAuthTable
ON articles.author = articleAuthTable.author_id
LEFT JOIN authors AS imageAuthTable
ON image.author = imageAuthTable.author_id
WHERE articles.categories = 'gamedev'
AND articles.article_id > 0
ORDER BY date DESC;
所以我在我的 php class 中实现了它(一如既往)并试图获得结果。它们是空的,结果是 "you have an error in you mysql"。 一如既往,我尝试将查询作为字符串回显并在 mysql 中重新运行它。奇迹般有效。在 php 中没有。同样的错误。
我试图将 sql 查询分成几部分,以查找错误,该错误似乎只存在于语句中:
[...] articles.article_id > 0
这会触发我的 php sql 查询崩溃。它在 php 中不会以任何方式工作。但在 mysql 中,它运行完美,我得到了正确的数据。 php 中的任何其他 SQL 查询都可以完美运行。
不起作用的部分应该限制结果。
编辑: PHP代码
public function queryWhile($query){
$query = htmlspecialchars($query);
$this->result = mysqli_query($this->db, $query);
if(!$this->result){
echo "No response";
$this->closeDB();
}else{
while ($row = mysqli_fetch_array($this->result)){
$this->resultArray[] = $row;
}
}
}
return $this->queryWhile("SELECT articles.article_id, articles.PreviewText, articles.date,articles.Title, articles.previewText, image.thumbnail, articleAuthTable.nickname AS authorNickname, imageAuthTable.nickname AS imageNickname FROM articles LEFT JOIN image ON articles.previewImage = image.image_id LEFT JOIN authors AS articleAuthTable ON articles.author = articleAuthTable.author_id LEFT JOIN authors AS imageAuthTable ON image.author = imageAuthTable.author_id WHERE articles.categories = '".$this->category."' ORDER BY date DESC;");
$query = htmlspecialchars($query);
正在转化
articles.article_id > 0
到
articles.article_id > 0
MySQL 显然不知道该做什么。在 SQL 语句中使用 htmlspecialchars
真的没有什么好的理由,除非您打算将其打印在网页上。