$batchImage = array();
$i = 0;
$sliderImages = $dbh->prepare("select image1, image2, image3, image4 from centres where centre_id = 1 ");
$sliderImages -> execute();
$images = $sliderImages -> fetch();
/*$img_1= $images['image1'];
$img_2 $images['image2'];
$img_3 $images['image3'];
$img_4 $images['image4'];*/
foreach($images as $image)
{
$batchImage[$i] = $image;
$i++;
}
foreach($batchImage as $im)
{
echo $im;
}
答案 0 :(得分:0)
$sliderImages->fetch()
返回的默认数组类型为PDO::FETCH_BOTH
。每个列都返回一个named和numeric索引。这就是为什么你要两次看到每张图片的原因。要获得数字索引,请使用PDO::FETCH_NUM
。
$sliderImages = $dbh->prepare("select image1, image2, image3, image4 from centres where centre_id = 1 ");
$sliderImages->execute();
$batchImage = $sliderImages->fetch(PDO::FETCH_NUM);
foreach($batchImage as $im)
{
echo $im;
}
您也可以使用PDO::FETCH_ASSOC
来获取指定的索引。