Re: Extracting random rows
| From: | Shivanischal | Date: | Mon, 03 Nov 2003 08:54:26 +0000 |
| Subject: | Re: Extracting random rows | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-168380@lists.php.net to get a copy of this message | ||
hi,
do try this - "select * from my_table orer by rand() limit my_max_rows" like
"select * from questions order by rand() limit 5". works for me. i'm
surprised too. but if u still want to do it in PHP, do go ahead.
warm regards,
-shiva
"Scott" <gildedpage@comcast.net> wrote in message
news:1067827213.2949.33.camel@localhost.localdomain...
> Hello,
>
> Below is a boiled-down example of the problem I'm having. I'm trying to
> extract rows randomly from MySql, but I keep getting the same one. It
> works if I query MySql directly from the command line, so it seems like
> a php problem.
>
> Thanks,
> Scott
>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
> <html>
> <head>
> <title></title>
> <meta http-equiv="Content-Type" content="text/html;
> charset=iso-8859-1">
> <meta name="GENERATOR" content="Quanta Plus">
> </head>
> <body>
>
> <?
>
> include ("connectlpi.php");
>
> $sql = "
> select id
> from $table_name
> order by rand()
> limit 1
> ";
>
> $result = @mysql_query($sql,$connection) or die("Couldn't execute
> query.");
>
> while ($row = mysql_fetch_array($result)) {
>
> $id = $row['id'];
>
> }
>
> echo "$id";
>
> ?>
>
>
>
> </body>
> </html>