有一个这样的表:
id imageid path tourid defaultimage
1 12 asd134 1 1
2 12 asd212 1 0
3 12 asd354 1 0
4 15 qwe 2 0
我需要使用imageid 12获取所有条目,并将所有条目放入不同的变量中。我可以为每个查询运行查询,但我永远不知道表中有多少条目具有相同的id。有时它是1,有时它很多。
我试过这个:
$result3 = mysqli_query($connect,"SELECT * FROM tourimages WHERE tourid='$tourid' AND defaultimage = '1'");
while($row3 = mysqli_fetch_array($result3)) {
$imagepath = $row3['imagepath'];
echo $imagepath;
}
$result4 = mysqli_query($connect,"SELECT * FROM tourimages WHERE tourid='$tourid' AND defaultimage = '0' LIMIT 1");
while($row4 = mysqli_fetch_array($result4)) {
$imagepath2 = $row4['imagepath'];
$image2id = $row4['imageid'];
echo $imagepath2 ;
}
$result5 = mysqli_query($connect,"SELECT * FROM tourimages WHERE tourid='$tourid' AND imageid!='{$image2id}' AND defaultimage = '0' LIMIT 1 ");
while($row5 = mysqli_fetch_array($result5)) {
$imagepath3 = $row5['imagepath'];
echo $imagepath3 ;
}
答案 0 :(得分:0)
$imagepath = array();
$result3 = mysqli_query($connect,"SELECT * FROM tourimages WHERE tourid='$tourid'");
while($row3 = mysqli_fetch_assoc($result3)) {
$imagepath[] = $row3['path'];
}
echo "<pre/>";print_r($imagepath);
在您想要的任何地方使用此$imagepath
数组。它对将来的参考也很有用。
答案 1 :(得分:0)
如果您需要变量,请尝试使用此代码: -
$i=1
while($row3 = mysqli_fetch_assoc($result3)) {
$imagepath.$i = $row3['imagepath'];
$i++
}
但好方法是使用数组: -
while($row3 = mysqli_fetch_assoc($result3)) {
$imagepath[] = $row3['imagepath'];
}
答案 2 :(得分:0)
试试这个
$result = mysqli_query($connect,"SELECT * FROM tourimages WHERE imageid='$imageid'");
$imagepath = [];
while($row3 = mysqli_fetch_array($result))
{
$imagepath[] = $row3['imagepath'];
}
print_r($imagepath);