下面的代码显示随机的数据库图像以及一个特定的图像(这就是我想要的)。我如何在数据库查询后混洗这些结果,因为图像ID 11总是首先显示?我希望图像ID 11在其他图像中随机显示。
我可以使用SHUFFLE()吗?如果是这样的话,我现在到底应该把它放在哪里?
任何帮助都将被视为仍在学习PHP
谢谢。
<?php
mysql_connect("", "", "") or die(mysql_error()) ;
mysql_select_db("images") or die(mysql_error()) ;
$photo=mysql_query("SELECT * FROM `profile_images` ORDER BY (ID = 11) DESC, RAND()
LIMIT 7");
while($get_photo=mysql_fetch_array($photo)){ ?>
<div style="width:300px;">
<img src="<? echo $get_photo['url']; ?>">
</div>
<? } ?>发布于 2012-02-27 04:57:05
您可以在将它们检索到php之后对它们进行混洗。
$photos = array();
while ($get_photo = mysql_fetch_array($photo))
$photos[] = $get_photo;
shuffle($photos);或者,您可以使用子查询完成此操作:
SELECT A.* FROM (
SELECT * FROM `profile_images`
ORDER BY (ID = 11) DESC, RAND()
LIMIT 7
) as A
ORDER BY RAND()https://stackoverflow.com/questions/9457029
复制相似问题