justlukeyou Posted June 5, 2012 Share Posted June 5, 2012 Hi, I am trying to echo 4 items from my DB but I have heard that it shouldn't be done using rand. http://php.net/manual/en/function.rand.php Is there anyway I can say: FROM productdbase random"); Or would that be to easy lol Quote Link to comment https://forums.phpfreaks.com/topic/263706-echo-random-correctly/ Share on other sites More sharing options...
litebearer Posted June 5, 2012 Share Posted June 5, 2012 http://davidwalsh.name/return-random-records-mysql Quote Link to comment https://forums.phpfreaks.com/topic/263706-echo-random-correctly/#findComment-1351401 Share on other sites More sharing options...
justlukeyou Posted June 5, 2012 Author Share Posted June 5, 2012 Hi, I have it like this so far but it creats the error below. <?php $query = mysql_query("SELECT * FROM productdbase RAND() LIMIT 1"); while($row = mysql_fetch_array($query)) { ?> <a href="/products/product.php?product_id=<?php echo $row['product_id']; ?>" class='productlink' rel="nofollow" ><?php echo $row['name']; ?></a> <?php } ?> mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/ukhomefu/public_html/indextest.php on line 169 Quote Link to comment https://forums.phpfreaks.com/topic/263706-echo-random-correctly/#findComment-1351403 Share on other sites More sharing options...
Mahngiel Posted June 5, 2012 Share Posted June 5, 2012 Your query is returning empty because you have improper code.. Practice your queries inside of PhpMyAdmin if you have that to ensure what you're going to use actually produces content. Try this instead SELECT * FROM `table` ORDER BY RAND() LIMIT 0,1; Quote Link to comment https://forums.phpfreaks.com/topic/263706-echo-random-correctly/#findComment-1351409 Share on other sites More sharing options...
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.