devdavad Posted June 16, 2008 Share Posted June 16, 2008 here's the bit of code that is giving me some problems function printjoke() { $result = mysql_query("SELECT * FROM jokes ORDER BY date DESC"); $i=0; while($row = mysql_fetch_array($result) && $i<1) { echo "<p class='joketitle'>". $row['date'] . "'s joke by " . $row['poster']; echo "<br /></p><p>"; echo $row['joke'] ."<br /><br /></p>"; $i++; } } function printarchive() { $result = mysql_query("SELECT * FROM jokes ORDER BY date DESC"); while($row = mysql_fetch_array($result)) { echo "<p class='joketitle'>". $row['date'] . "'s joke by " . $row['poster']; echo "<br /></p><p>"; echo $row['joke'] ."<br /><br /></p>"; } } basically I'd like to have the newest joke entry in the table jokes be displayed on the index.php page that will call the printjoke(); function while having all of the other jokes being printed on the archive.php page. the only problem is calling printjoke(); just prints 's joke by does anyone know what might be the problem? Quote Link to comment https://forums.phpfreaks.com/topic/110460-solved-almost-finished-my-first-php-mysql-page/ Share on other sites More sharing options...
wildteen88 Posted June 16, 2008 Share Posted June 16, 2008 You need to add LIMIT 1 to your query. Without this your query will select all records in the jokes table: $result = mysql_query("SELECT * FROM jokes ORDER BY date DESC LIMIT 1"); As your query only returns one result you do not need to use a while loop to display the query results. So you printJokes function will now be: function printjoke() { $result = mysql_query("SELECT * FROM jokes ORDER BY date DESC LIMIT 1"); $row = mysql_fetch_array($result); echo "<p class='joketitle'>". $row['date'] . "'s joke by " . $row['poster']; echo "<br /></p><p>"; echo $row['joke'] ."<br /><br /></p>"; } Quote Link to comment https://forums.phpfreaks.com/topic/110460-solved-almost-finished-my-first-php-mysql-page/#findComment-566734 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.