Web hosting plans - WRITE YOUR OWN BLOG ENGINE } else {

WRITE YOUR OWN BLOG ENGINE } else { echo “

There are no blog posts in the database.

“; } ?> Stepping through the code: first a connection to the database is opened as before. Then an SQL statement is constructed to pull all the posts out of the database: SELECT post_id, title, DATE_FORMAT(postdate, ‘%e %b %Y at %H:%i’) . AS dateattime FROM posts ORDER BY postdate DESC I introduced a couple of new functions here. First, the ORDER BY clause at the end of the query sorts all the posts into date order with the newest first. You can order by more than one field at a time by using a comma-separated list. For example this clause would sort by postdate first and then by title for those posts with the same date: ORDER BY postdate DESC, title ASC The other new function is DATE_FORMAT. Because the postdate field was created with a data type of DATETIME, MySQL can manipulate the date and time data it couldn t do this if the date were stored as normal text. DATE_FORMAT allows you to output a date and time exactly as you wish. In this case, the postdate, which is currently stored as something like 2005-04-03 15:30:00, is formatted and displayed as 3 Apr 2005 at 15:30. An alias for the formatted date is created using AS dateattime (this is for convenience, as will become apparent later in this section). More information about the date_format function can be found on the MySQL website at http://dev.mysql.com/doc/refman/4.1/en/date-and-time-functions.html. Moving on to the main body of the HTML document, I have inserted a chunk of PHP after the main heading. This script steps through all the posts retrieved from the database in the afore-mentioned SELECT query and displays them in a list with links through to the edit page. Here s how: \n”; Firstly the code checks that the $myposts array is not null; in other words, it checks that there are actually some posts to display. If so, it opens an HTML ordered list. do { .. } while ($myposts = mysql_fetch_array($result)); Then a do-while loops through all the posts returned by the SELECT query. A do-while loop repeats everything within it until the while expression is no longer true. The key here is that every time the mysql_fetch_array function is executed it advances to the next row in the query result thus populating the $myposts array with the next post pulled from the 293

WordPress database error: [Table 'armadillowebhosting_com_-_apache.wp_comments' doesn't exist]
SELECT * FROM wp_comments WHERE comment_post_ID = '364' AND comment_approved = '1' ORDER BY comment_date

Leave a Reply