jj20051 Posted October 4, 2008 Share Posted October 4, 2008 I Made The Code Bellow To Display Every A Row In A Database. Now I Would Like To Make It So That It Displays Only One Random Row... Here Is My Code: $sqlstr = mysql_query("SELECT * FROM products"); if (mysql_numrows($sqlstr) != 0) { while ($row = mysql_fetch_array($sqlstr)) { ?> <p><?= $row['name'] ?></p> <p><?= $row['description'] ?></p> <p><img src="<?= $row['image'] ?>"></p> <?php } } P.S. - Help With Code Would Be Great Since I'm Not that Great At Visualizing PHP Code In My Head Yet.. but Whatever Help You Do Give Thanks In Advance! Link to comment https://forums.phpfreaks.com/topic/126987-solved-random-row-display/ Share on other sites More sharing options...
xtopolis Posted October 4, 2008 Share Posted October 4, 2008 http://www.phpfreaks.com/forums/index.php/topic,125759.0.html Link to comment https://forums.phpfreaks.com/topic/126987-solved-random-row-display/#findComment-656889 Share on other sites More sharing options...
Brandon Jaeger Posted October 4, 2008 Share Posted October 4, 2008 This should do the trick: $query = "SELECT * FROM products ORDER BY rand() LIMIT 1" $result = mysql_query($query); $row = mysql_fetch_row($result); echo '<p>'.$row['name'].'</p>'; echo '<p>'.$row['description'].'</p>'; echo '<p><img src="'.$row['image'].'"></p>'; Link to comment https://forums.phpfreaks.com/topic/126987-solved-random-row-display/#findComment-656890 Share on other sites More sharing options...
JasonLewis Posted October 4, 2008 Share Posted October 4, 2008 Not mysql_fetch_row(), mysql_fetch_assoc(). mysql_fetch_row() returns a numerical array, so you would need to access them with 0 and 1 and 2 instead of by their name. Link to comment https://forums.phpfreaks.com/topic/126987-solved-random-row-display/#findComment-656891 Share on other sites More sharing options...
Brandon Jaeger Posted October 4, 2008 Share Posted October 4, 2008 Oops! Thanks for pointing that out. $query = "SELECT * FROM products ORDER BY rand() LIMIT 1" $result = mysql_query($query); $row = mysql_fetch_assoc($result); echo '<p>'.$row['name'].'</p>'; echo '<p>'.$row['description'].'</p>'; echo '<p><img src="'.$row['image'].'"></p>'; I'm not sure why I did that, but its fixed now Link to comment https://forums.phpfreaks.com/topic/126987-solved-random-row-display/#findComment-656894 Share on other sites More sharing options...
jj20051 Posted October 4, 2008 Author Share Posted October 4, 2008 Thanks Everyone. Especially Project Fear ( Who Solved The Confusing Part ) and Brandon Jaeger For Providing The Code that Got Me Confused, and Later Fixed It... Link to comment https://forums.phpfreaks.com/topic/126987-solved-random-row-display/#findComment-656895 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.