mysql两个数组组合,在一个数组中合并两个MySQL查询

这篇博客探讨了如何从同一表中获取一个真实图像和三个随机生成的图像,并以随机顺序显示,供用户(儿童)选择正确的图像。文章提供的SQL查询和PHP代码示例展示了如何实现这一目标,同时提醒不要使用已弃用的mysql_函数,推荐使用MySQLi或PDO。
摘要由CSDN通过智能技术生成

Purpose of this query is to retrieve a true image plus 3 random generated images from same table, then show them randomly. User(child) have to select correct image.

Thanks

$sql= "SELECT * FROM `login` WHERE `user` = '$word'";

" UNION"

"SELECT * FROM `login` WHERE `user` != '$word' ORDER BY RAND() LIMIT 3";

$row2=mysql_query($sql);

$i=1;

while ($r = mysql_fetch_array($row2))

{

echo '

';

echo ''.%24r%5B'img'%5D.'
';

echo $r['user'];

echo '

';

$i++;

}

解决方案$sql = "(SELECT * FROM `login` WHERE `user` = '$word')";

$sql.= " UNION";

$sql.= " (SELECT * FROM `login` WHERE `user` != '$word' ORDER BY RAND() LIMIT 3)";

$sql.= " ORDER BY RAND()";

To get the results you can use for example (poseted before OP added his code with mysql_* functions):

$MySQL=new mysqli("localhost", "username", "password", "database");

$query = $MySQL -> query($sql);

while ($entry = $query -> fetch_row())

{

// $entry is an array with the results, use for example:

echo $entry[0]; // will return the first column of your table

echo $entry[1]; // will return the second column of your table

// try also:

var_dump($entry); // outputs everything for testing purposes

}

Please, don't use the mysql_* functions, they are deprecated and will be removed in the future versions of PHP. Use MySQLi or PDO instead. See Why shouldn't I use mysql_* functions in PHP? for more details.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值