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?

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.