Jump to content

How do I make a random query?


3raser

Recommended Posts

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'>";
}

Link to comment
Share on other sites

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'>";
}

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. :/

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

-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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.