Jump to content


How to randomise selected?

  • Please log in to reply
3 replies to this topic

#1 Wayder

  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 18 April 2003 - 09:35 PM


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 :)

#2 benW

  • Members
  • PipPip
  • Member
  • 12 posts
  • LocationWaukesha WI

Posted 19 April 2003 - 12:51 AM

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())  

<?php str_replace("ASP", "PHP", $str); ?>

#3 Wayder

  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 19 April 2003 - 03:18 PM

Thanks ben :D I tried that, but it does not do what I need.

You see, the code first randomises, then restricts to the first six. What I need to do, is to restrict to the first six, then randomise only the six.

#4 Cereal

  • Members
  • Pip
  • Newbie
  • 2 posts
  • LocationBelgium

Posted 19 April 2003 - 04:22 PM

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>


i hope this helps:)
Windows = a 64 bit rewrite for a 32 bit extension to a 16 bit GUI on an 8 bit OS written for a 4 bit architecture by a 2 bit company who can\'t stand 1 bit of competition.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users