Jump to content

How to randomise selected?


Wayder

Recommended Posts

Hi,

 

I need to randomise the selected rows.

 

$sql = mysql_query(\"SELECT col1, col2, col3, col4 FROM table WHERE col2 = \'$a\' or col3 = \'$b\' or col4 = \'$ip\' limit 0,6\") or die(mysql_error())

 

I have my first six rows. How do I randomise the 6 selected rows?

 

Thanks :)

Link to comment
Share on other sites

Use MySQL\'s built in random function :D


$sql = mysql_query("SELECT col1, col2, col3, col4 FROM table WHERE col2 = \'$a\' or col3 = \'$b\' or col4 = \'$ip\' ORDER BY rand() limit 0,6") or die(mysql_error())  

Link to comment
Share on other sites

use2 sql querie.

 

the first one gets the max number off rows in the table, and saves this number into an var.

then use the randomze function from php to generate an random number.

 

now run an second sql querie that also limits the 6 collums (or whatever), and specify in the query the number you let php generate.

 

and you have an random record out off ythe table.

 

this is an example coe that i use.


$q = "SELECT COUNT(*) AS c FROM news WHERE typ = \'news\' AND approved = 1 AND theme <> \'$sms_subj\'";

$result = mysql_db_query($edge_database,$q,$mysql);

$row = mysql_fetch_object($result);

$rnd = rand(1,$row->c);

$q = "SELECT * FROM news WHERE id = $rnd";

$result = mysql_db_query($edge_database,$q,$mysql);

$row = mysql_fetch_object($result);

echo "<p class=edgenormal>";

edge_do_title("Random old article");

?>

<center><a href=newsread.php?newsid=<? echo $row->id; ?>><? echo $row->title; ?></a></center>

</p>

 

i hope this helps:)

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.