簡體   English   中英

PHP 變量不適用於 WHERE 子句

[英]PHP variable is not working with WHERE clause

當我在 WHERE 子句中使用變量時,我的查詢不起作用。 我已經嘗試了一切。 我回顯變量 $res,它向我顯示了完美的值,當我在查詢中使用該變量時,查詢沒有獲取任何東西,因此 mysqli_num_rows 給我零值,但是當我給出該變量靜態包含的值時,查詢完美執行。 我已經多次使用相同類型的代碼並且它運行良好,但是現在在模塊的這一部分中它不起作用。

代碼:

$res = $_GET['res']; // I have tried both post and get
echo $res; //here it echos the value = mahanta
$query = "SELECT * FROM `seller` WHERE `restaurant` = '$res'"; // Here it contains the problem I have tried everything. Note: restaurant name is same as it is in the database $res contains a value and also when I give the value of $res i.e. mahanta in the query it is then working.
$z = mysqli_query($conn, $query);
$row2 = mysqli_fetch_array($z);
echo var_dump($row2); // It is giving me null
$num = mysqli_num_rows($z); // Gives zero

if ($num > 0) {
    while ($row2 = mysqli_fetch_array($z)) {
        $no = $row2['orders'];
        $id = $res . $no;
    }
}
else {
    echo "none selected";
}

正如評論中所討論的那樣。 通過打印查詢var_dump($query) ,您將獲得發送到數據庫進行查詢的確切語法。

調試提示:您還可以通過將 var_dump($query) 值粘貼到數據庫中來進行測試,如果您的查詢沒有問題,您將看到結果。

因此,更新您的查詢語法並打印查詢將對您有所幫助。

$query = "SELECT * FROM `seller` WHERE `restaurant` = '$res'";
var_dump($query);

希望這對您和將來的新手有所幫助,了解如何測試您的查詢。

建議:另請參閱如何編寫 mysql 查詢語法以更好地理解mysql 查詢中的 php 變量

問題在於您在查詢中使用$res的方式。 使用.$res代替。 在 PHP(本機或框架)中,將變量注入查詢需要正確的語法。

暫無
暫無

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

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