問題描述
當 photoId 直接在語句上而不是變量時,以下工作完全沒有問題.
The following works with no problem at all, when the photoId is directly on the statement and not a variable.
$img_query = mysqli_query($con, 'SELECT * FROM imgs WHERE photoid = "103"') or die(mysqli_error($con));
但是下面的操作不會出錯,可能是什么原因導致無法選擇.
but the following just won't work with no error, what might be causing this not to select.
$imageid = '103';
$img_query = mysqli_query($con, 'SELECT * FROM imgs WHERE photoid = "$imageid"') or die(mysqli_error($con));
$img_row = mysqli_fetch_array($img_query);
echo $img_row['img'];
這是一個while循環.
This is inside a while loop.
while($row = mysqli_fetch_array($somequery)){
$imageid = $row['photoid'];
$img_query = mysqli_query($con, 'SELECT * FROM imgs WHERE photoid = "$imageid"') or die(mysqli_error($con));
$img_row = mysqli_fetch_array($img_query);
echo $img_row['img'];
}
謝謝.
推薦答案
在php中'
和"
差別很大,查詢語法是雙引號左右變量周圍的查詢和單引號......盡管我建議您在查詢中使用參數,而不是直接將變量放入查詢中
in php a '
and a "
are very different and the query syntax is double quote around the query and single quote around variables.. although I would recommend you look at using parameters on your query instead of just putting a variable directly into the query
$imageid = '103';
$query = $con->prepare("SELECT * FROM imgs WHERE photoid = ?");
$query->bind_param('sssd', $imageid);
$query->execute();
這只是它的細節......如果你想了解更多關于連接的信息......錯誤處理和其他一切閱讀DOCS
this is just the nuts and bolts of it... if you want more information about the connection.. error handling and everything else read the DOCS
這篇關于如何在另一個查詢的 while 循環中運行 sql 查詢的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!