Jump to content

Selecting Random record


Grant Holmes

Recommended Posts

I'm trying to pull any random record from my table. I had code (modified below) that was pulling all the active records (see commented row around line 4). I searched the forum and found this post

 

I tried to use that to modify my search, but am getting an error: "Incorrect arguments to RAND"

 

 

<?
include("datatube/dbinfo.inc.php");
mysql_connect($dbhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
//$query="SELECT * from SongList where active='1'";
$result = mysql_fetch_row(mysql_query("SELECT * from SongList where active='1' ORDER BY RAND(id)")  or die(mysql_error()));
$row = mysql_fetch_array( $result2 ) or die(mysql_error());
$num=mysql_numrows($result); 
mysql_close();
?>
<CENTER>
<table border="1" cellspacing="2" cellpadding="3" class="sortable" width="90%">
<?
$i=0;
while ($i < $num) {
$Active=mysql_result($result,$i,"Active");
$SongTitle=mysql_result($result,$i,"SongTitle");
$ArtistPrimary=mysql_result($result,$i,"ArtistPrimary");
$SongPace=mysql_result($result,$i,"SongPace");
$id=mysql_result($result,$i,"id"); 
if($SongPace == "")  $SongPace = " "; 
?>

      <tr>   
            <TD valign="top"><? echo "$SongTitle"; ?></TD>
            <TD valign="top"><? echo "$ArtistPrimary"; ?></TD>				   	   
            <TD valign="top"><? echo "$SongPace"; ?></TD>	   		   
            </TD>
      </tr>
<?
++$i;
} 
echo "</table></CENTER>";
?>

Link to comment
https://forums.phpfreaks.com/topic/91226-selecting-random-record/
Share on other sites

Caught one error in my $request, line: 6

fixed that, but still getting same error:

 

<?
include("datatube/dbinfo.inc.php");
mysql_connect($dbhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
//$query="SELECT * from SongList where active='1'";
$result = mysql_fetch_row(mysql_query("SELECT * from SongList where active='1' ORDER BY RAND(id)")  or die(mysql_error()));
$row = mysql_fetch_array( $result ) or die(mysql_error());
$num=mysql_numrows($result); 
mysql_close();
?>
<CENTER>
<table border="1" cellspacing="2" cellpadding="3" class="sortable" width="90%">
<?
$i=0;
while ($i < $num) {
$Active=mysql_result($result,$i,"Active");
$SongTitle=mysql_result($result,$i,"SongTitle");
$ArtistPrimary=mysql_result($result,$i,"ArtistPrimary");
$SongPace=mysql_result($result,$i,"SongPace");
$id=mysql_result($result,$i,"id"); 
if($SongPace == "")  $SongPace = " "; 
?>

$result = mysql_fetch_row(mysql_query("SELECT * from SongList where active='1'  and id='$id' ORDER BY RAND()")  or die(mysql_error()));
$row = mysql_fetch_array( $result ) or die(mysql_error());

 

 

isinit just rand

$result = mysql_fetch_row(mysql_query("SELECT * from SongList where active='1' ORDER BY RAND()")  or die(mysql_error()));
$row = mysql_fetch_array( $result ) or die(mysql_error());

This is the result:

 

<?
include("datatube/dbinfo.inc.php");
mysql_connect($dbhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$result = mysql_query("SELECT * from SongList where active='1' ORDER BY RAND() LIMIT 1")   or die(mysql_error());
$row = mysql_fetch_array( $result ) or die(mysql_error());
$num=mysql_numrows($result); 
mysql_close();
?>
<CENTER>
<table border="1" cellspacing="2" cellpadding="3"  width="90%">
<?
$i=0;
while ($i < $num) {
$Active=mysql_result($result,$i,"Active");
$SongTitle=mysql_result($result,$i,"SongTitle");
$ArtistPrimary=mysql_result($result,$i,"ArtistPrimary");
$SongPace=mysql_result($result,$i,"SongPace");
$id=mysql_result($result,$i,"id"); 
if($SongPace == "")  $SongPace = " "; 
?>

      <tr>   
            <TD valign="top"><? echo "$SongTitle"; ?></TD>
            <TD valign="top"><? echo "$ArtistPrimary"; ?></TD>				   	   
            <TD valign="top"><? echo "$SongPace"; ?></TD>	   		   
            </TD>
      </tr>
<?
++$i;
} 
echo "</table></CENTER>";
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.