hey everyone, again i am very new to php but i am learning very fast. Asp.net is still my baby but i love php. Anyhow i have a little problem.Is there a way to make this more random ? $result = mysql_query('SELECT * FROM pictures ORDER BY RAND() LIMIT 1');
$result = mysql_query("SELECT * FROM `pictures` ORDER BY `id` DESC") //replace id with your primary key $x = 0; while ($row = mysql_fetch_array($result)){ $ids[$x] = $row['id']; $x++; } asort($ids); $random_id = rand($ids[0],$ids[(count($ids) - 1)]); $final_result = mysql_query("SELECT * FROM `pictures` WHERE `id`='$random_id'"); PHP: Edit: I think I got this mixed up, did you want to display all the pictures in a random order? According to (http://bugs.mysql.com/bug.php?id=817), this bug has been fixed. Have you tried updating your MySQL?
Using rand() is very slow and will only get slower as your database grows. What do you mean "more random"? There are a multitude of ways of fetching a random row from a database, a few examples are here; http://akinas.com/pages/en/blog/mysql_random_row/ You can also process the randomization through PHP rather than the database itself.
If your database is small,you should use RAND() otherwise try another solutions as like mentiones in the link that lukeg gave you
Dear Andy, Me too, I would process the randomization through PHP: you can - first retrieve the rows from DB - then use the prebuilt shuffle function from PHP: php.net/manual/en/function.shuffle.php Good luck
hi there, I'm looking for something similar, I want to select a random row and update an specific field. say tbl_images and fld_show is = by default, if set it show=1; the image shows. so I want to select a random row and enable 1 pic every time I run the page.
My experience with rand under mysql is awful. Depending on the size of your table and your indexes, the result can just be one loooonnngg wait until you have the result back. If your table has an ID field (auto increment primary key type thing), I would choose a randon number under php and call that number out. Pseudo code would be like : $number = rand(0,10000); $sql = "select * from whatever where id='$number'"; $result = mysql_query...etc PHP:
hi, socalcoder answer is the best one answer for your Question AndyFarrell ! and after where condition you can also limit your records, so have you tried it ?