Grant Holmes Posted February 15, 2008 Share Posted February 15, 2008 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 More sharing options...
Grant Holmes Posted February 15, 2008 Author Share Posted February 15, 2008 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 https://forums.phpfreaks.com/topic/91226-selecting-random-record/#findComment-467580 Share on other sites More sharing options...
redarrow Posted February 15, 2008 Share Posted February 15, 2008 $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 https://forums.phpfreaks.com/topic/91226-selecting-random-record/#findComment-467587 Share on other sites More sharing options...
Grant Holmes Posted February 15, 2008 Author Share Posted February 15, 2008 Redarrow, thanks for the reply. I just got it fixed. Maybe its too early!!! Thanks! What happened to the "Solved" button?? Link to comment https://forums.phpfreaks.com/topic/91226-selecting-random-record/#findComment-467588 Share on other sites More sharing options...
redarrow Posted February 15, 2008 Share Posted February 15, 2008 post the result mate cheers... Link to comment https://forums.phpfreaks.com/topic/91226-selecting-random-record/#findComment-467589 Share on other sites More sharing options...
Grant Holmes Posted February 15, 2008 Author Share Posted February 15, 2008 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 https://forums.phpfreaks.com/topic/91226-selecting-random-record/#findComment-467602 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.