0

I've been looking for this for a while but with no success.

I am trying to implement a recomendation bar, for example like in youtube, when you are seeing a video it shows the list or recommended videos on the right.

At this moment I am using this method:

$offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `$tablename` ");
$offset_row = mysql_fetch_object($offset_result ); 
$offset = $offset_row->offset;
$result_rand = mysql_query( " SELECT * FROM `$tablename` LIMIT $offset, 9 " );

This works fine, but sometimes doesn't show any result, and the problem is also that its not completely random, because it shows for example the first ID as 200, so the next result will be id 201 and then 202 and so.

I would like to know if there is a way to show this 9 randon results, for example 1º result id 500, 2º result id 10, 3º result id 788, etc etc?

Thank you

1
  • post your code in a better format Commented Oct 24, 2012 at 0:55

4 Answers 4

1

Not entirely sure this answers what you are looking for, but try:

$result_rand = mysql_query("SELECT * FROM " . $tablename . " ORDER BY RAND() LIMIT 9");

Sign up to request clarification or add additional context in comments.

3 Comments

This will work, however note the ORDER BY RAND() can take a while to process if you have many rows within your table
Yes I dont wanna work with ORDER BY RAND() because my table will get quite big and this way will take too much time...
@HogardelOcio - In that case, you should probably use a modified version of Angelo A's code. I would get the count of records in the table, then generate a set number of IDs to select from the database. This also allows you to reject one of the random IDs if it is the same as the one currently being displayed on the page.
0

You can use php rand() function to create 5 numbers and save them in an array:

http://php.net/manual/en/function.rand.php

<?php
$rand_array = array();
for($i=1;$i<5;$i++) {
    $rand_array[$i] = rand(0,500);
}
?>

and after that create a query with every int with a foreach loop and work with your data.

<?php
foreach ($rand_array as $integer) {
     $q = "SELECT * from $tablename WHERE id='$integer';";
}
?>

Does this helps?

2 Comments

This is an intresting solution, but my table have holes in the ids, because of some I have to delete. Is there a way to do not count ids that dont exists?
Thank you I will addapt it for my needs.
0

First you should use mysqli_ functions instead of mysql_ because the latter is deprecated. Second use order by rand() to get random rows:

$rand_result = mysqli_query( "SELECT * FROM $tablename ORDER BY RAND() LIMIT 9;" );

Comments

0

UNTESTED:

SELECT id, @rownum:=@rownum+1 AS rownum, name FROM users u, (SELECT @rownum:=0) r

THis will give a unique number to each row in sequence. Now if you create a temp table with 9 random numbers between 1 and count(*) of your table and JOIN those two together...

Not sure about performance but seems like it might be faster than Rand and order by since I only need 9 random numbers

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.