The Problem

I have a db.php file which contains mysql login details. I want it in a separate file like this so it can be linked in several different pages for easier tracing later.

This all works when not on a wordpress site, but I'm trying to convert everything over to wordpress now and I'm encountering a problem.

The below doesn't work (echoes nothing on page), but if I copy the single line from inc/db.php into my page-pricechanges.php it works.

What's the best way for me to link this?



  • \page-pricechanges.php
  • \header-pricechanges.php
  • \inc\db.php

Page code


        include 'inc/db.php'; 
        //I have also tried include get_bloginfo("template_url")'/inc/db.php';
        //I have tried include 'db.php' and put db.php in root of site, root of theme folder.. nothing seems to pick it up.


    $con=mysqli_connect("localhost","root","", "dbname");


$query1 = mysqli_query($con, "SELECT * FROM dbname ORDER BY balance DESC") 
    or die(mysql_error());
while ($row = mysqli_fetch_assoc($query1))
        $id = $row['id'];
        $name = addslashes($row['name']);
        echo $id." ".$name."<br>";

Wordpress has its own connection class, which can be used trough the $wpdb object.

Is there a particular reason to use direct queries to the db, instead the wordpress functions (get_posts, get_post_meta, get_option)? Keep in mind that by doing this, you loose a lot of the wordpress tools, such as cache, filters, etc...

  • The database I listed isn't a wordpress one. It's a fantasy football stats database. All I'm trying to do is create a custom page which shows all the numbers from the MySQL Database in a HTML table. I didn't know about $wpdb... Can I set this up for my own databases? I'll check it out now. – Cully Jun 11 '13 at 0:57
  • 1
    That can also be done with the wpdb class: wordpress.stackexchange.com/a/1618/33558 – davidmh Jun 11 '13 at 0:59
  • Great thanks - I wasn't aware. I'll take a look now. That looks like it will solve my problem. – Cully Jun 11 '13 at 1:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.