PHP and Movable Type and MySQL Play Nice

Mon, Jan 20, 2003 with tags old , php , mysql

I’m glad that I started using the MySQL stuff for movable type. I just wrote snippet of code that selects the five most recent entries from my blog and produces links for them. Here is the code (where it’s assumed $GLOBALS[“dbh”] is a handle to the PEAR database handle):

function mtLink($_entryId, $_entryTitle, $_entryDate) {
    echo "<li><a href=\"weblog/archives/";
    for ($i = (6 - strlen($_entryId)); $i > 0; $i --) {
        echo "0";
    echo $_entryId.".html\">".$_entryTitle."</a><br />\n";
    echo date("F j, Y, g:ia",$_entryDate)."</li>\n";

function latestWeblogs() {
    $dbh = $GLOBALS["dbh"];
    $stmt = "  SELECT entry_id, entry_title, unix_timestamp(entry_created_on)
                 FROM mt_entry
             ORDER BY entry_created_on desc
                LIMIT 5";
    $result = $dbh->query($stmt);
    while ($thisrow = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
        mtLink($thisrow["entry_id"], $thisrow["entry_title"], $thisrow["unix_timestamp(entry_created_on)"]-3600*6);

The 3600*6 is to put everything in my timezone. It’s not flaweless and may get stuff off by an hour or two, but works well enough. This will, by default just print list items, so you’ll want to encase it in a <ul> or >ol> or something like that. You can see the results on the sides of most of my pages.