airjunkie2000 Posted March 14, 2006 Posted March 14, 2006 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? Here's the code: <?php $result = mysql_query("SELECT media_id, thumbnail, file, path FROM media_info WHERE media_id = RAND() LIMIT 3") or die(mysql_error()); while($row=mysql_fetch_array($result)) { ?> <a href="<?=$row[path];?>?media_id=<?=$row[media_id];?>"><img src="<?=$row['thumbnail']?>" alt="" width="100" height="80"></a></td> <br> <? } ?> Thanks for any help Quote
deanavail Posted March 15, 2006 Posted March 15, 2006 how about frst listing everything in $row then reformat the output in a print statement as indicated below: ><?php $result = mysql_query("SELECT media_id, thumbnail, file, path FROM media_info WHERE media_id = RAND() LIMIT 3") or die(mysql_error()); // test what is in $row // print "<table=border=1>\n"; while($row=mysql_fetch_array($result)) { print "<tr>\n"; foreach ($row as $field=>$val ) print "<td>$field</td><td>$val</td>"; print "</tr>\n"; } Print "</table><br>\n"; // reformat results in a print statement // while($row=mysql_fetch_array($result)) { print "<a href=\".$row['path']."?media_id=".$row['media_id']."><img src=\".$row['thumbnail'].">alt=\"\" width=\"100\" height=\"80\"></a><br> } ?> Quote
minapre999 Posted March 15, 2006 Posted March 15, 2006 Hi Airjunkie 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()". maybe replace the where clause with "media_id = (SELECT media_id FROM media_info order by RAND() limit 3 )" might work? Peter 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? Here's the code: <?php $result = mysql_query("SELECT media_id, thumbnail, file, path FROM media_info WHERE media_id = RAND() LIMIT 3") or die(mysql_error()); while($row=mysql_fetch_array($result)) { ?> <a href="<?=$row[path];?>?media_id=<?=$row[media_id];?>"><img src="<?=$row['thumbnail']?>" alt="" width="100" height="80"></a></td> <br> <? } ?> Thanks for any help Quote
airjunkie2000 Posted March 15, 2006 Author Posted March 15, 2006 Thanks for the input I will try both these suggestions Quote
airjunkie2000 Posted March 15, 2006 Author Posted March 15, 2006 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. Quote
deanavail Posted March 15, 2006 Posted March 15, 2006 airjnkie2000, 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. >// test what is in $row // print "<table=border=1>\n"; while($row=mysql_fetch_array($result)) { print "<tr>\n"; foreach ($row as $field=>$val ) print "<td>$field</td><td>$val</td>"; print "</tr>\n"; } Print "</table><br>\n"; Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.