問題描述
當(dāng) photoId 直接在語句上而不是變量時(shí),以下工作完全沒有問題.
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));
但是下面的操作不會(huì)出錯(cuò),可能是什么原因?qū)е聼o法選擇.
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'];
這是一個(gè)while循環(huán).
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中'
和"
差別很大,查詢語法是雙引號(hào)左右變量周圍的查詢和單引號(hào)......盡管我建議您在查詢中使用參數(shù),而不是直接將變量放入查詢中
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();
這只是它的細(xì)節(jié)......如果你想了解更多關(guān)于連接的信息......錯(cuò)誤處理和其他一切閱讀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
這篇關(guān)于如何在另一個(gè)查詢的 while 循環(huán)中運(yùn)行 sql 查詢的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!