随机播放MYSQL结果

编程入门 行业动态 更新时间:2024-10-28 08:30:06
本文介绍了随机播放MYSQL结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以下代码显示随机数据库图像以及一个特定的图像(这是我想要的).在数据库查询之后,由于始终首先显示图像ID 11,我该如何洗牌这些结果?我希望图像ID 11随机显示在其他图像之中.

The following code displays random database images as well as one specific image (which is what I want). How can I shuffle these results after database query as image ID 11 is always displayed first? I want image ID 11 displayed randomly amongst the others.

我可以使用SHUFFLE()吗?如果是这样,那么我现在该把它放在哪里?

Can I use SHUFFLE(). If so where exactly do I put it as at the moment?

任何帮助仍然可以学习PHP

Any help would be appreciated as still learning 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> <? } ?>

推荐答案

在将它们检索到php后,您可以对其进行洗牌.

You can shuffle them after they are retrieved to php.

$photos = array(); while ($get_photo = mysql_fetch_array($photo)) $photos[] = $get_photo; shuffle($photos);

或者您可以通过子查询来做到这一点:

Or you can do it with subqueries:

SELECT A.* FROM ( SELECT * FROM `profile_images` ORDER BY (ID = 11) DESC, RAND() LIMIT 7 ) as A ORDER BY RAND()

更多推荐

随机播放MYSQL结果

本文发布于:2023-10-28 08:12:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1536051.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:MYSQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!