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
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 = " "; 
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>";
?>

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.