Small Mysql Problem

liunx

Guest
Im sure Im missing something obvious here, but its still not working correct. Im trying to display 3 random images from a database... I don't get any coding errors, parse errors, but it displays nothing?<br /><br />Here's the code:<br /><br /><?php<br /><br />$result = mysql_query("SELECT media_id, thumbnail, file, path FROM media_info WHERE media_id = RAND() LIMIT 3")<br />or die(mysql_error());<br /><br />while($row=mysql_fetch_array($result))<br />{<br />?><br /><a href=http://www.totalchoicehosting.com/forums/lofiversion/index.php/"<?=$row[path];?>?media_id=<?=$row[media_id];?>"><img src="<?=$row['thumbnail']?>" alt="" width="100" height="80"></a></td><br /> <br><br /><? <br />}<br />?><br /><br />Thanks for any help<!--content-->
how about frst listing everything in $row then reformat the output in a print statement as indicated below: <br /><!--fonto:tahoma--><span style="font-family:tahoma"><!--/fonto--><!--sizeo:1--><span style="font-size:8pt;line-height:100%"><!--/sizeo--><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><?php<br />$result = mysql_query("SELECT media_id, thumbnail, file, path FROM media_info WHERE media_id = RAND() LIMIT 3") or die(mysql_error());<br /><br />//  test what is in $row <br />//<br />print "<table=border=1>\n";<br />while($row=mysql_fetch_array($result)) {<br />     print "<tr>\n";<br />     foreach ($row as $field=>$val )<br />     print "<td>$field</td><td>$val</td>";<br />     print "</tr>\n";<br />}<br />Print "</table><br>\n";<br /><br />// reformat results in a print statement<br />//<br />while($row=mysql_fetch_array($result)) {<br />print "<a href=http://www.totalchoicehosting.com/forums/lofiversion/index.php/\".$row['path']."?media_id=".$row['media_id']."><img src=\".$row['thumbnail'].">alt=\"\" width=\"100\" height=\"80\"></a><br><br />}<br />?><!--c2--></div><!--ec2--><!--sizec--></span><!--/sizec--><!--fontc--></span><!--/fontc--><!--content-->
Hi Airjunkie<br /><br />I haven't used a lot of the mysql functions, but have had a look at the mysql manual, which says RAND() returns a random floating-point value in the range from 0 to 1.0. You need to make sure that there is a row in the database for media_id, which appears unlikely for the statment "media_id = RAND()".<br /><br />maybe replace the where clause with "media_id = (SELECT media_id FROM media_info order by RAND() limit 3 )" might work?<br /><br />Peter <br /><br /><br /><br /><br /><!--quoteo(post=169773:date=Mar 15 2006, 09:44 AM:name=airjunkie2000)--><div class='quotetop'>QUOTE(airjunkie2000 @ Mar 15 2006, 09:44 AM) <a href="http://www.totalchoicehosting.com/forums/index.php?act=findpost&pid=169773"><img src='http://www.totalchoicehosting.com/forums/style_images/1/post_snapback.gif' alt='*' border='0' /></a></div><div class='quotemain'><!--quotec-->Im sure Im missing something obvious here, but its still not working correct. Im trying to display 3 random images from a database... I don't get any coding errors, parse errors, but it displays nothing?<br /><br />Here's the code:<br /><br /><?php<br /><br />$result = mysql_query("SELECT media_id, thumbnail, file, path FROM media_info WHERE media_id = RAND() LIMIT 3")<br />or die(mysql_error());<br /><br />while($row=mysql_fetch_array($result))<br />{<br />?><br /><a href=http://www.totalchoicehosting.com/forums/lofiversion/index.php/"<?=$row[path];?>?media_id=<?=$row[media_id];?>"><img src="<?=$row['thumbnail']?>" alt="" width="100" height="80"></a></td><br /> <br><br /><? <br />}<br />?><br /><br />Thanks for any help<!--QuoteEnd--></div><!--QuoteEEnd--><!--content-->
Thanks for the input I will try both these suggestions<!--content-->
Turns out you were right about the rand statement, it was retuning a media_id that did not exist in the database, so instead I just user ORDER BY RAND() and then displayed 3 results, thanks both of you.<!--content-->
airjnkie2000,<br /><br />not sure if you tried out the modified script i provided you but note that you would know that your $result is empty by printing out its contents to verify it. It is a good idea to add this block of code whenever you are troubleshooting calls to mysql. <br /><!--fonto:Tahoma--><span style="font-family:Tahoma"><!--/fonto--><!--sizeo:1--><span style="font-size:8pt;line-height:100%"><!--/sizeo--><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->//  test what is in $row<br />//<br />print "<table=border=1>\n";<br />while($row=mysql_fetch_array($result)) {<br />     print "<tr>\n";<br />     foreach ($row as $field=>$val )<br />     print "<td>$field</td><td>$val</td>";<br />     print "</tr>\n";<br />}<br />Print "</table><br>\n";<!--c2--></div><!--ec2--><!--sizec--></span><!--/sizec--><!--fontc--></span><!--/fontc--><!--content-->
 
Back
Top