Title: WordPress Working With Some Simple Code
Last modified: August 18, 2016

---

# WordPress Working With Some Simple Code

 *  [acoppard1994](https://wordpress.org/support/users/acoppard1994/)
 * (@acoppard1994)
 * [19 years, 3 months ago](https://wordpress.org/support/topic/wordpress-working-with-some-simple-code/)
 * I have the following code displaying on my home page
 *     ```
       <?php 
   
       // connect to database
       $link= mysql_connect("localhost", "USERID", "PASSWORD");
        if (! $link ){
         $dberror= 'Server not available';
         print $dberror;
         return false;
       }
       mysql_select_db('DBNAME', $link);
       if (! mysql_select_db('DBNAME_WP_1', $link)){
         $dberror= mysql_error();
         print $dberror;
         return false;
       }
       $query = "SELECT post_name, post_title, post_content
       FROM wp_posts
       WHERE post_status = 'publish' and post_date <= now()
       ORDER BY post_date DESC
       LIMIT 0 , 5 ";
       $result = mysql_query($query);
       if ( ! $result){
            $dberror = mysql_error();
            print $dberror;
            return false;
        } else {
       //write to page a linked Title and the content. 
   
            while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
         printf("<span><a href='/YOURBLOGURL/%s'>» %s</a> </span> <br />%s<br /><br />",
            $row["post_name"], $row["post_title"], $row["post_content"]);
            } 
   
       }
       ?>
       ```
   
 * which displays the most recent 5 posts. It works properly, displays all the posts
   but the link does not work. I have changed the url. The script thinks that it
   should be [http://www.mydomain.com/journal/postname](http://www.mydomain.com/journal/postname)
   
   vbut my installation of WordPress 2.1 says that the post should be at [http://www.mydomain.com/journal/?id=#](http://www.mydomain.com/journal/?id=#)
 * Any help??

Viewing 1 replies (of 1 total)

 *  [timok](https://wordpress.org/support/users/timok/)
 * (@timok)
 * [19 years, 3 months ago](https://wordpress.org/support/topic/wordpress-working-with-some-simple-code/#post-528990)
 *     ```
       $query = "SELECT ID, post_name, post_title, post_content
       FROM wp_posts
       WHERE post_status = 'publish' and post_date <= now()
       ORDER BY post_date DESC
       LIMIT 0 , 5 ";
       ```
   
 * …
 *     ```
       while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
         printf("<span><a href='/YOURBLOGURL/?id=%s'>» %s</a> </span> <br />%s<br /><br />",
           $row["ID"], $row["post_title"], $row["post_content"]);
       }
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘WordPress Working With Some Simple Code’ is closed to new replies.

 * 1 reply
 * 2 participants
 * Last reply from: [timok](https://wordpress.org/support/users/timok/)
 * Last activity: [19 years, 3 months ago](https://wordpress.org/support/topic/wordpress-working-with-some-simple-code/#post-528990)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
