My problem is pretty much self explanatory but I cant quite work it out to make it as efficient as possible.
I want to select a random entry from a MySQL database. I want it to be as fast as possible and as efficient as possible (that's always the goal, isn't it?). When I select that row I want to select another row, but not the same as the one before. If I select 10 rows I want the 11th row to be different from all others (lets say unique :) ). But when I run out of rows I want to "report an error".
To get to the heart of the problem. I am using PHP with MySQL. I have an input array containing titles which have already been selected. Then I get the count of all items in the database so I know how many times can I "loop through max". Lets paste the code to see what we're dealing with here.
try
{
$db = new PDO("mysql:host=localhost;dbname=xxxxx;charset=utf8", "xxxx", "xxxx");
$played = explode(":;:", $_POST['items']); //All already selected items are in $_POST separated by :;:
$sql = "SELECT count(id) AS count FROM table"; //Lets get the total count of items
$query = $db->prepare($sql);
$query->execute();
$result = $query->fetch(PDO::FETCH_ASSOC);
$count = $result['count']; //There we are...
$i = 0; //Index counter so we dont exceed records.. well kinda (more on that below)
do //do while because we want something to be selected first
{
$sql = "SELECT FLOOR(RAND() * COUNT(*)) AS offset FROM table"; //From here
$query = $db->prepare($sql);
$query->execute();
$result = $query->fetch(PDO::FETCH_ASSOC);
$offset = $result['offset'];
$sql = "SELECT itemXML FROM table LIMIT $offset, 1";
$query = $db->prepare($sql);
$query->execute();
$result = $query->fetch(PDO::FETCH_ASSOC); //To here is some code to randomly select a record "as efficiently as possible"..
$output = Array();
$xml = simplexml_load_string($result['itemXML']);
$i++;
} while (in_array($xml->{"title"}, $played) && $i < $count); //While record title is in array and while we have not exceeded the total number of records (that's the logic but it isint that simple is it?)
if ($i >= $count)
{
die("400"); //Just a random status code which I parse with the client.
}
$itemArr = Array("whatever" => $xml->{"whatever-attr"}, "title" => $xml->{"title"});
array_push($output, $itemArr); Lets push this to out array
echo json_encode($output); //Aaaaand finally lets print out the results
}
catch (Exception $e) //If anything went wrong lets notify our client so he can respond properly
{
$output = Array("error" => $e->getMessage());
die(json_encode($output));
}
Yes well.. The problem is that WHAT IF there are 10 records, 9 rows have been selected and the index counter $i
gets bigger or equal 10 and random records are all in the array. Then we have one row that should have been selected but its not.
And how do I fix this? Your help will be much appreciated!
If I didnt explain it well enough let me know an I will try harder.