Jump to content

Listing Numerous Entries from MySQL Database


Mundo

Recommended Posts

While I've done alot of Java/C++ programming in the past, I've only recently started using PHP and MySQL.

I have a simple "news" table, that contains ID, Title, Date, Entry and URL to and Image. I've managed to setup some code to display 1 item from the table, but how can I get it to list ALL or the 10 latest entires?

 

Any simple tutorial appreciated. It doesn't need to be too complicated.

Thanks! :)

Link to comment
Share on other sites

What part of this is difficult for you?

Do you need help with MySQL query to get last 10 items?

Or with PHP code to display several results from one query?

Or something else? ;)

 

Anyway, for query it would be something like

SELECT * FROM table ORDER BY timeEntered DESC LIMIT 10

 

For php code, pretty good example is in manual entry for mysql_query

Link to comment
Share on other sites

<?
mysql_connect("localhost","root","");
mysql_select_db("ncfc");
$query = mysql_query('SELECT * FROM news LIMIT 10');
echo $query;
mysql_close();
?> 

 

When I use this, I just get "Resource id #3" posted to screen?

Link to comment
Share on other sites

<?
include "tpl/header.tpl";
include "tpl/navigation.tpl";

mysql_connect("localhost","root","");
mysql_select_db("ncfc");

$query = sprintf("SELECT * FROM news LIMIT 10");
$result = mysql_query($query);

if (!$result) {
$message  = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
while ($row = mysql_fetch_assoc($result)) {
echo '<div id="entryheader"><span style="float: left;" id="entryheadertext">(#' . $row['news_ID'] . ') <strong>' . $row['news_Title'] . ' </strong></span><small style="float: right; padding-right: 12px; padding-top: 3px;">' . $row['news_Date'] . '</small></div>';
echo '<div id="entrybody"><img src="' . $row['news_Picture'] . '" style="float: right; padding-left: 12px; padding-top: 0px;" />' . $row['news_Entry'] . '</div><br/>';
}

mysql_free_result($result);
mysql_close();

include "tpl/footer.tpl";
?>

 

Ok, this is my code at the moment. It works fine, but I feel "echo-ing" out my HTML is bad practise? Is there a more suitable way or outputting it?

 

 

Link to comment
Share on other sites

Guest
This topic is now 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.