3raser Posted March 10, 2011 Share Posted March 10, 2011 I want to select 20 sites randomly. But when I generate a random number and use it in my query to extract the site information. But if an ID doesn't exist with that random number, it doesn't display information. How can this be fixed? $amount_get = mysql_query("SELECT id FROM users"); $random = rand(0,mysql_num_rows($amount_get)); for($amount = $random; $amount > 30; $amount = $random) { $query = mysql_query("SELECT site_url,username,id FROM users WHERE id='$amount' LIMIT 150"); $data_grab = mysql_fetch_assoc($query); echo $data_grab['site_url']."<input type='submit'>"; } Quote Link to comment https://forums.phpfreaks.com/topic/230172-how-do-i-make-a-random-query/ Share on other sites More sharing options...
MadLittleMods Posted March 10, 2011 Share Posted March 10, 2011 That will order your sites randomly so you can just display the first one... $amount_get = mysql_query("SELECT id FROM users"); $query = mysql_query("SELECT site_url,username,id FROM users WHERE 1 ORDER BY RAND() LIMIT 150"); $data_grab = mysql_fetch_assoc($query); echo $data_grab['site_url']."<input type='submit'>"; } Quote Link to comment https://forums.phpfreaks.com/topic/230172-how-do-i-make-a-random-query/#findComment-1185370 Share on other sites More sharing options...
cssfreakie Posted March 10, 2011 Share Posted March 10, 2011 I read somewhere that the use of rand in a sql query is extremly slow , certainly when it has quite some records maybe try to get the numerical id of the last row of you table (i assume you have such a primary key right? ) select id from users order by id desc limit 1; after that you know how many rows you have and you can let php create a random result. put those in order and use that to set up a query to retrieve 20 row's from the database. Quote Link to comment https://forums.phpfreaks.com/topic/230172-how-do-i-make-a-random-query/#findComment-1185375 Share on other sites More sharing options...
3raser Posted March 10, 2011 Author Share Posted March 10, 2011 I have a question about the following code: for($count = 0; $count < 30; $count += 1) { $query = mysql_query("SELECT site_url,username,id FROM users ORDER BY RAND() LIMIT 150"); $data_grab = mysql_fetch_assoc($query); echo "<a href='verify.php?id=". $data_grab['id'] ."'>". $data_grab['site_url'] ."</a><br/>"; } Is it possible to make it so it doesn't output the same site over and over? I have 2 sites in my database, and it just does the same sites over and over. :/ Quote Link to comment https://forums.phpfreaks.com/topic/230172-how-do-i-make-a-random-query/#findComment-1185439 Share on other sites More sharing options...
cssfreakie Posted March 10, 2011 Share Posted March 10, 2011 i hope you realize that if you had 1 million rows in your database that code would take much longer than the idea i suggested. besides that you are pulling 150 rows from the table but only iterate over 30 times. that is exactly 5 times to many rows you pul from the database. IF you need 30 rows set the limit of the for loop to 30. now back to your question: in the event that you out put those 30 rows in your forloop are all those echoes the same? or are they the same as the result shown after you refreshed the page? If that last thing is the case maybe use sessions to keep track of the results. Maybe have a look at your code and what i said above with getting the last row to know the number of rows etc. -edit: oh sorry i looked at your for last code, this new code you shown, if i am correct it will always show the first from the array. maybe use array_shuffle() before you output it. but i would not use that code really(i gave a suggestion already which i would) Quote Link to comment https://forums.phpfreaks.com/topic/230172-how-do-i-make-a-random-query/#findComment-1185442 Share on other sites More sharing options...
3raser Posted March 10, 2011 Author Share Posted March 10, 2011 -edit: oh sorry i looked at your for last code, this new code you shown, if i am correct it will always show the first from the array. maybe use array_shuffle() before you output it. but i would not use that code really(i gave a suggestion already which i would) What do you mean by always showing the first? And does array_shuffle() keep rows from coming up out of the for loop twice? http://urlexchange.zxq.net/ That for example. How do I stop from the same row coming out multiple times? Quote Link to comment https://forums.phpfreaks.com/topic/230172-how-do-i-make-a-random-query/#findComment-1185847 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.