我可能会错过明显的东西,如何返回所需的结果?看一个简单的例子:
$fetch_image = "SELECT image_url FROM table"; //returns about 10 rows
$stmt_img = $db->prepare($fetch_image);
$stmt_img->execute();
$stmt_img_result = $stmt_img->get_result();
while ($row_img = $stmt_img_result->fetch_assoc()) {
$image = $row_image['image_url'];
echo 'Image url: '.$image.'<br>'; //this will echo out 10 times.
}
上面的代码遍历所有10行。如何从结果集中返回第一行,然后再返回其他9行?
编辑:我正在尝试创建一个图像预览器,以使第一个图像显示为占位符/主图像,然后其他9行显示为缩略图,如下所示:
<div class="sp-wrap">
<a href="images/1.jpg">
<!-- This would be the placeholder/main/first image from the mysql result set -->
<img src="images/1_tb.jpg" alt="">
</a>
<!-- In real life these would be dynamically generated -->
<a href="images/3.jpg"><img src="images/3_tb.jpg" alt=""></a>
<a href="images/4.jpg"><img src="images/4_tb.jpg" alt=""></a>
<a href="images/5.jpg"><img src="images/5_tb.jpg" alt=""></a>
<a href="images/6.jpg"><img src="images/6_tb.jpg" alt=""></a>
</div>
答案 0 :(得分:1)
您可以尝试获取第一行,然后获取其余行。但是您需要在SQL语句中使用ORDER BY
子句,该子句将在每次执行时以相同的方式对行进行排序。
<?php
$fetch_image = "SELECT image_url FROM table"; //returns about 10 rows
$stmt_img = $db->prepare($fetch_image);
$stmt_img->execute();
$stmt_img_result = $stmt_img->get_result();
// Fetch first row
if ($row_first = $stmt_img_result->fetch_assoc()) {
$image = $row_first['image_url'];
echo 'Image url: '.$image.'<br>';
}
// Fetch next rows
while ($row_next = $stmt_img_result->fetch_assoc()) {
$image = $row_next['image_url'];
echo 'Image url: '.$image.'<br>';
}
?>
另一种可能的方法是对第一行实施自定义检查:
<?php
$fetch_image = "SELECT image_url FROM table"; //returns about 10 rows
$stmt_img = $db->prepare($fetch_image);
$stmt_img->execute();
$stmt_img_result = $stmt_img->get_result();
$first = true;
while ($row_img = $stmt_img_result->fetch_assoc()) {
if ($first) {
// Specific output for the first row
$first = false;
$image = $row_image['image_url'];
echo 'Image url: '.$image.'<br>'; // this will echo out 1 time.
} else {
// Specific output for the next rows
$image = $row_image['image_url'];
echo 'Image url: '.$image.'<br>'; // this will echo out count - 1 times.
}
}
?>
答案 1 :(得分:1)
一个简单的布尔变量可以区分第一行:
$first_row = true;
while ($row_img = $stmt_img_result->fetch_assoc()) {
if ($first_row) {
echo 'the first row';
$image = $row_image['image_url'];
echo 'Image url: '.$image.'<br>';
$first_row = false;
} else {
$image = $row_image['image_url'];
echo 'Image url: '.$image.'<br>'; //this will echo out 9 times.
}
}
答案 2 :(得分:-1)
在表中放置imageType Column
,然后使用此查询
SELECT image_url FROM table order by imageType`
将值1赋予要在循环中首先显示的图像。
如果第一行具有第一张图像,则不需要imageType列。您可以简单地使用
SELECT image_url FROM table order by id
id是您的主键
但是,上述方法还是更动态的方法,以防将来您的记录发生更改。最好给他们提供imageType列以供将来使用,这是更合适的方法,而不是仅仅从数据库中获取第一条记录。
$stmt_img = $db->prepare($fetch_image);
$stmt_img->execute();
$stmt_img_result = $stmt_img->get_result();
if ( $firstrow = mysql_fetch_assoc($stmt_img_result) ) {
$image = $firstrow['image_url'];
echo 'Image url: '.$image.'<br>'; //this will echo out 1 times.
while($row = mysql_fetch_assoc($stmt_img_result)) {
$image = $row['image_url'];
echo 'Image url: '.$image.'<br>'; //this will echo out 9 times.
}
}