Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?


Structure

\THEMEFOLDER

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

Page code

header-pricechanges.php

<head>
    <?php  
        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.
    ?>
</head>

inc/db.php

<?php
    $con=mysqli_connect("localhost","root","", "dbname");
?>

page-pricechanges.php

$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>";
    }
share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

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...

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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