gpt4 book ai didi

php - 在wamp上使用php从mysql数据库中随机选择

转载 作者:行者123 更新时间:2023-11-29 10:40:54 24 4
gpt4 key购买 nike

我正在尝试制作一种反击破案的开案。我的问题是,我怀疑原因是 wamp,它无法加载,或者需要很长时间,以至于我无法得到任何结果。我阅读了所有关于使用 mysqli 从数据库中选择随机项目的文章,它确实很慢并且不适合使用,但我不认为它会这么慢。所以我真的想知道这是否真的是导致问题的原因,或者是否存在我遗漏的完全不同的错误。预先感谢:)

if(isset($_GET['iid'])) {

$iid = rString($_GET['iid']);

$checkOwnerQuery = $data->query('SELECT * FROM inventory WHERE id = "'.$iid.'" AND ownerId = "'.$id.'"');
$checkOwner = $checkOwnerQuery->num_rows;
if ($checkOwner == 1) {
// OWNER CONFIRMED, CONTINUE OPENING
$randomNmbr = mt_rand(1, 100);
if($randomNmbr == 1) {
while($row = $data->query('SELECT * FROM caserewards WHERE chance = "1" ORDER BY RAND() LIMIT 1')->fetch_assoc()) {

$rewardId = $row['id'];
$rewardName = $row['name'];
$rewardLore = $row['lore'];
$rewardType = $row['type'];
$rewardAmount = $row['amount'];
$rewardChance = $row['chance'];
$rewardColor = $row['color'];
$rewardImg = $row['img'];
$rewardActive = $row['active'];

}
echo $rewardName;

} else if($randomNmbr >= 97) {

while($row = $data->query('SELECT * FROM caserewards WHERE chance = "97" ORDER BY RAND() LIMIT 1')->fetch_assoc()) {

$rewardId = $row['id'];
$rewardName = $row['name'];
$rewardLore = $row['lore'];
$rewardType = $row['type'];
$rewardAmount = $row['amount'];
$rewardChance = $row['chance'];
$rewardColor = $row['color'];
$rewardImg = $row['img'];
$rewardActive = $row['active'];

}
echo $rewardName;

} else if($randomNmbr >= 95) {

while($row = $data->query('SELECT * FROM caserewards WHERE chance = "95" ORDER BY RAND() LIMIT 1')->fetch_assoc()) {

$rewardId = $row['id'];
$rewardName = $row['name'];
$rewardLore = $row['lore'];
$rewardType = $row['type'];
$rewardAmount = $row['amount'];
$rewardChance = $row['chance'];
$rewardColor = $row['color'];
$rewardImg = $row['img'];
$rewardActive = $row['active'];
}
echo $rewardName;

} else if($randomNmbr >= 90) {

while($row = $data->query('SELECT * FROM caserewards WHERE chance = "90" ORDER BY RAND() LIMIT 1')->fetch_assoc()) {

$rewardId = $row['id'];
$rewardName = $row['name'];
$rewardLore = $row['lore'];
$rewardType = $row['type'];
$rewardAmount = $row['amount'];
$rewardChance = $row['chance'];
$rewardColor = $row['color'];
$rewardImg = $row['img'];
$rewardActive = $row['active'];
}
echo $rewardName;

} else if($randomNmbr >= 80) {

while($row = $data->query('SELECT * FROM caserewards WHERE chance = "80" ORDER BY RAND() LIMIT 1')->fetch_assoc()) {

$rewardId = $row['id'];
$rewardName = $row['name'];
$rewardLore = $row['lore'];
$rewardType = $row['type'];
$rewardAmount = $row['amount'];
$rewardChance = $row['chance'];
$rewardColor = $row['color'];
$rewardImg = $row['img'];
$rewardActive = $row['active'];
}
echo $rewardName;

} else if($randomNmbr >= 60) {

while($row = $data->query('SELECT * FROM caserewards WHERE chance = "60" ORDER BY RAND() LIMIT 1')->fetch_assoc()) {

$rewardId = $row['id'];
$rewardName = $row['name'];
$rewardLore = $row['lore'];
$rewardType = $row['type'];
$rewardAmount = $row['amount'];
$rewardChance = $row['chance'];
$rewardColor = $row['color'];
$rewardImg = $row['img'];
$rewardActive = $row['active'];
}
echo $rewardName;

} else if($randomNmbr >= 50) {

goto a;

} else if($randomNmbr <= 50 && $randomNmbr != 1) {

a:

while($row = $data->query('SELECT * FROM caserewards WHERE chance = "50" ORDER BY RAND() LIMIT 1')->fetch_assoc()) {

$rewardId = $row['id'];
$rewardName = $row['name'];
$rewardLore = $row['lore'];
$rewardType = $row['type'];
$rewardAmount = $row['amount'];
$rewardChance = $row['chance'];
$rewardColor = $row['color'];
$rewardImg = $row['img'];
$rewardActive = $row['active'];
}
echo $rewardName;

} else {
echo 'Major danger';
}
echo '<div class="item_img_center" style="background-image: url()"></div>';

} else {
header('Location: inventory.php');
}
} else {
header('Location: inventory.php');
}

最佳答案

我认为您不希望在随机查询周围使用 while 循环,因为每次返回结果时都会重新运行查询(即永远,因为它们将始终返回除非 caserewards 完全为空,否则为结果)。

尝试仅使用:

$row = $data->query('SELECT * FROM caserewards WHERE chance = "50" ORDER BY RAND() LIMIT 1')->fetch_assoc();

$rewardId = $row['id'];
$rewardName = $row['name'];
$rewardLore = $row['lore'];
$rewardType = $row['type'];
$rewardAmount = $row['amount'];
$rewardChance = $row['chance'];
$rewardColor = $row['color'];
$rewardImg = $row['img'];
$rewardActive = $row['active'];

对所有其他查询进行类似的更新。

关于php - 在wamp上使用php从mysql数据库中随机选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45506633/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com