如何在另一个查询的while循环内运行sql查询
当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();
这只是它的基本内容...如果您想了解有关连接的更多信息..错误处理和其他所有内容,请阅读
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