我已经创建了一个sql查询,并希望以随机顺序显示数据,就像每次刷新它应该调出随机数据一样
这是我的代码
<?php
if(isset($_POST['new_list'])) {
$class_id = $_POST['get_class_id'];
$user_id = $_POST['user_id'];
$sql2 = mysqli_query($connection, "SELECT * FROM get_help WHERE class = '$class_id' AND status = 'Pending' AND user_id != '$user_id' ORDER BY id ASC LIMIT 1");
while($record = mysqli_fetch_assoc($sql2)) {
$sql3 = mysqli_query($connection, "SELECT * FROM users WHERE uid = '".$record['user_id']."' AND btc_email != ''");
if(mysqli_num_rows($sql3)) {
$data2 = mysqli_fetch_array($sql3);
?>
<tr class="odd">
<td><?php echo $data2["first_name"] . " " . $data2["last_name"]; ?></td>
<td><?php echo $data2["email"]; ?></td>
<td><?php echo $data2["phone"]; ?></td>
<td>
<p>
Click donate to see receipient bitcoin address. Input Hash ID after transfer
</p>
</td>
<td>
<div class="col-md-2"></div>
<div class="col-md-2">
<button type="button" onclick="get_details(<?php echo $record['id']; ?>);" name="donate" class="btn btn-red">Donate</button>
</div>
</td>
</tr>
<?php } ?>
<?php } ?>
<?php } ?>
答案 0 :(得分:2)
您可以使用ORDER BY RAND()来代替在第一个查询中使用ORDER BY ID。这将为您的显示生成随机结果:
$sql2 = mysqli_query($connection, "SELECT * FROM get_help WHERE class = '$class_id' AND status = 'Pending' AND user_id != '$user_id' ORDER BY RAND() LIMIT 1");
请记住在$ _POST数据周围使用htmlentities()以确保它对数据库查询是安全的,例如:
$class_id = htmlentities($_POST['get_class_id']);
$user_id = htmlentities($_POST['user_id']);