Extracting random rows

This is a discussion on Extracting random rows within the PHP General forums, part of the PHP Programming Forums category; Hello, Below is a boiled-down example of the problem I'm having. I'm trying to extract rows randomly ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-03-2003
Scott
 
Posts: n/a
Default Extracting random rows

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>
Reply With Quote
  #2 (permalink)  
Old 11-03-2003
Marek Kilimajer
 
Posts: n/a
Default Re: [PHP] Extracting random rows

Hello, works for me. As a workaround you can pass a random integer to te
RAND() function to be used as seed value.

Scott wrote:
> 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>
>

Reply With Quote
  #3 (permalink)  
Old 11-03-2003
Shivanischal
 
Posts: n/a
Default Re: Extracting random rows

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.localdomai n...
> 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>

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:23 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0