简体   繁体   English

PHP随机行帮助

[英]Php random row help

I've created some code that will return a random row, (well, all the rows in a random order) But i'm assuming its VERY uneffiecent and is gonna be a problem in a big database... 我创建了一些将返回随机行的代码,(但是,所有行都以随机顺序),但是我假设它的代码效率很低,并且在大型数据库中将是一个问题...

Anyone know of a better way? 有人知道更好的方法吗?

Here is my current code: 这是我当前的代码:

$count3 = 1;
$count4 = 1;
//Civilian stuff...
$query = ("SELECT * FROM `*Table Name*` ORDER BY `Id` ASC");
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$count = $count + 1;
$civilianid = $row['Id'];
$arrayofids[$count] = $civilianid;
//echo $arrayofids[$count];
}

while($alldone != true) {
$randomnum = (rand()%$count) + 1;
//echo $randomnum . "<br>";
//echo $arrayofids[$randomnum] . "<br>";
$currentuserid = $arrayofids[$randomnum];
$count3 += 1;

while($count4 < $count3) {
$count4 += 1;
$currentarrayid = $listdone[$count4];
//echo "<b>" . $currentarrayid . ":" . $currentuserid . "</b> ";
if ($currentarrayid == $currentuserid){
$found = true;
//echo " '" .$found. "' ";
}
}

if ($found == true) {
//Reset array/variables...
$count4 = 1;
$found = false;
} else {
$listdone[$count3] = $currentuserid;
//echo "<u>" . $count3 .";". $listdone[$count3] . "</u> ";
$query = ("SELECT * FROM `*Tablesname*` WHERE Id = '$currentuserid'");
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$username = $row['Username'];
echo $username . "<br>";
$count4 = 1;
$amountdone += 1;
if ($amountdone == $count) { //$count
$alldone = true;
}
}
}

Basically it will loop until its gets an id (randomly) that hasnt been chosen yet. 基本上,它将循环直到获得尚未被选择的ID(随机)。 -So the last username could take hours :P -所以最后一个用户名可能要花几个小时:P

Is this 'bad' code? 这是“不良”代码吗? :P :( :P :(

You could delegate that to MySQL: 您可以将其委托给MySQL:

SELECT * FROM table_name ORDER BY RAND() LIMIT 1;

It will return a random row from your table, which I guess should be more efficient than your php solution. 它将从您的表中返回一个随机行,我想应该比您的php解决方案更有效。 Nevertheless, it would still be slow within a large database. 但是,在大型数据库中,它仍然很慢。

You may be interested in checking out the following Stack Overflow posts for alternative solutions: 您可能有兴趣查看以下Stack Overflow帖子中的替代解决方案:

You could either change the query to give you all the results in a random order... 您可以更改查询以按随机顺序为您提供所有结果...

$query = ("SELECT * FROM `*Table Name*` ORDER BY RAND()");

Then just display them all in a simple loop. 然后将它们全部显示在一个简单的循环中。

Or you could get all the results into an array, then randomise the order in the array. 或者,您可以将所有结果放入数组中,然后将数组中的顺序随机化。 You can use the php shuffle() function for that. 您可以使用php shuffle()函数。 http://www.php.net/manual/en/function.shuffle.php http://www.php.net/manual/zh/function.shuffle.php

I would assume that the first option would give you the best results, but the correct solution will be to measure the performance and try to optimise if it is "too slow". 我认为第一种选择将为您带来最佳结果,但是正确的解决方案将是衡量性能并尝试优化它是否“太慢”。

However, picking a random element from your array, seeing if you have done that one already and trying again if you have is horrific . 但是,从数组中选择一个随机元素,看看是否已经完成该操作,然后再尝试一次,这是很可怕的 Do anything except that. 除此之外,什么都不要做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM