Jump to content

[SOLVED] almost finished my first php mysql page


devdavad

Recommended Posts

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?

Link to comment
Share on other sites

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>";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.