2015-12-22 67 views
0

即时通讯遇到一个相当奇怪的错误。sql只适用于mysql,但不适用于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类(一如既往),并试图获得的结果。它们是空的,导致“你在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;"); 
+1

因此,向我们展示PHP/MySQL连接等。 –

+1

您正在使用的PHP代码? – LeonardChallis

+1

日期是保留字。你不应该将它用于你的列名,但是既然你已经这样做了,你应该把这个字段用反引号括起来。 –

回答

4
$query = htmlspecialchars($query); 

被转换

articles.article_id > 0 

articles.article_id > 0 

的MySQL显然不知道该怎么做。除非您打算在网页上打印出来,否则我认为在SQL语句中使用htmlspecialchars确实没有什么好的理由。

+0

这是您的答案。 –

+0

谢谢,你的回答是对的。我对htmlspecialchars有一种误解。 – TheDomis4

相关问题