Mundo Posted February 7, 2009 Share Posted February 7, 2009 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 https://forums.phpfreaks.com/topic/144236-listing-numerous-entries-from-mysql-database/ Share on other sites More sharing options...
Mchl Posted February 7, 2009 Share Posted February 7, 2009 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 https://forums.phpfreaks.com/topic/144236-listing-numerous-entries-from-mysql-database/#findComment-756993 Share on other sites More sharing options...
Mundo Posted February 8, 2009 Author Share Posted February 8, 2009 Well my current query is: SELECT * FROM news LIMIT 10 But it only gives me 1 entry from the table, not the last 10... Link to comment https://forums.phpfreaks.com/topic/144236-listing-numerous-entries-from-mysql-database/#findComment-757672 Share on other sites More sharing options...
Mchl Posted February 8, 2009 Share Posted February 8, 2009 Even if you run it through MySQL console? Perhaps the query does return ten rows, but your PHP code is only displaying one. Link to comment https://forums.phpfreaks.com/topic/144236-listing-numerous-entries-from-mysql-database/#findComment-757698 Share on other sites More sharing options...
Mundo Posted February 8, 2009 Author Share Posted February 8, 2009 Yeh I think that is the problem, my PHP is only returning one result... Would I just use a loop or? Link to comment https://forums.phpfreaks.com/topic/144236-listing-numerous-entries-from-mysql-database/#findComment-757700 Share on other sites More sharing options...
Mchl Posted February 8, 2009 Share Posted February 8, 2009 Yes. See the link I posted before. It has very good example of using while loop to display several rows from query. Link to comment https://forums.phpfreaks.com/topic/144236-listing-numerous-entries-from-mysql-database/#findComment-757704 Share on other sites More sharing options...
Mundo Posted February 8, 2009 Author Share Posted February 8, 2009 <? 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 https://forums.phpfreaks.com/topic/144236-listing-numerous-entries-from-mysql-database/#findComment-757706 Share on other sites More sharing options...
Mchl Posted February 8, 2009 Share Posted February 8, 2009 Yes. See the link I posted before. It has very good example of using while loop to display several rows from query. Link to comment https://forums.phpfreaks.com/topic/144236-listing-numerous-entries-from-mysql-database/#findComment-757709 Share on other sites More sharing options...
Mundo Posted February 8, 2009 Author Share Posted February 8, 2009 Yes, sorry, I posted that before I read. I understand now. It's actually pretty simple. Thanks for the help. Link to comment https://forums.phpfreaks.com/topic/144236-listing-numerous-entries-from-mysql-database/#findComment-757711 Share on other sites More sharing options...
Mchl Posted February 8, 2009 Share Posted February 8, 2009 Glad I could help Link to comment https://forums.phpfreaks.com/topic/144236-listing-numerous-entries-from-mysql-database/#findComment-757714 Share on other sites More sharing options...
Mundo Posted February 8, 2009 Author Share Posted February 8, 2009 <? 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 https://forums.phpfreaks.com/topic/144236-listing-numerous-entries-from-mysql-database/#findComment-757725 Share on other sites More sharing options...
Mchl Posted February 8, 2009 Share Posted February 8, 2009 In short, there are several. That's question for another topic in another part of the forum though. Link to comment https://forums.phpfreaks.com/topic/144236-listing-numerous-entries-from-mysql-database/#findComment-757731 Share on other sites More sharing options...
Recommended Posts