簡體   English   中英

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類中實現了它(一如既往)並嘗試獲取結果。 它們為空,並導致“ 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 ,除非您打算將其打印在網頁上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM