-1

Basically I am trying to create a class to handle Database connections. I have double checked to make sure that my connection variables are correct and they work if I plug them directly into the PHP page. But when I call the class like this:

require_once __DIR__ . 'includes/db_connect.php';
$db = new DB_CONNECT();

It doesn't work. Here is my code. Maybe I am missing something.

<?php

// Class file to connect to database
class DB_CONNECT {

    // constructor
    function __construct()
    {
        // connection to database
        $this->connect();
    }

    // destructor
    function __destruct()
    {
        // disconnecting from database
        $this->close();
    }

    // Function for Database Connection
    function connect()
    {
        // import database connection variables
        require_once _DIR_ . '/db_config.php';

        // connection to mysql database
        $con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die (mysql_error());

        // selecting database
        $db = mysql_select_db(DB_DATABASE) or die (mysql_error()) or die (mysql_error());

        // returning connection cursor
        return $con;
    }

    // Function to close db connection
    function close()
    {
        // closing db connection
        mysql_close();
    }

}
?>
DonOfDen
  • 3,968
  • 11
  • 62
  • 112
cp-stack
  • 785
  • 3
  • 17
  • 40
  • Its just a comment, but why reinvent the wheel when you could be using an ORM ? http://stackoverflow.com/questions/108699/good-php-orm-library – Bjoern Rennhak May 23 '13 at 14:54
  • 1
    What do you mean by "it doesn't work"? – Aleks G May 23 '13 at 14:54
  • 1
    Please explain what you expect to happen, what is really happening, and what you have tried to fix it. Include any error messages or log information that may be relevant. – George Cummins May 23 '13 at 14:55
  • 1
    Your example call includes `require_once __DIR__...`; your class includes `require_once _DIR_...` – andrewsi May 23 '13 at 14:55
  • This isn't right: `or die (mysql_error()) or die (mysql_error())` – George Cummins May 23 '13 at 14:56
  • is this `db_config.php`? if not: show it – ClydeFrog May 23 '13 at 14:57
  • Can you verify that `__DIR__` is defined? It is new to php 5.3.0. Furthermore as @andrewsi said the `__DIR__` in your function is missing a set of underscores. – ajon May 23 '13 at 15:01
  • 2
    @BjoernRennhak, he's not even reinventing an ORM - he's only reinventing PDO... except without exceptions and using the deprecated mysql extension. – Bill Karwin May 23 '13 at 15:01
  • 1
    If you're still writing your first few database lines, stop what you're doing with `mysql_` functions and move to `mysqli_` - `mysql_` is deprecated. – jterry May 23 '13 at 15:01
  • @jterry so should I go ahead and replace all the mysql_ instances with mysqli_ ? – cp-stack May 23 '13 at 15:36
  • It's not quite that simple, but you're not far off. There are lots of resources out there, like [this question](http://stackoverflow.com/questions/4640520/changing-this-from-mysql-to-mysqli), to start. – jterry May 23 '13 at 18:07

3 Answers3

1

There's a typo in your connect() function:

require_once _DIR_ . '/db_config.php';

Should be:

require_once __DIR__ . '/db_config.php';

You need the double underscores, otherwise PHP will be looking in the root directory of the server for the config file.

andrewsi
  • 10,807
  • 132
  • 35
  • 51
0

Try something like this

    function connect(){
        $this->conn = mysqli_connect("localhost",$this->user,$this->password);
        if (!$this->conn){
            echo "No DB connection"; 
            exit;
        }
        $this->db = mysqli_select_db("DB_NAME");
        if (!$this->db){
            echo "Can't find DB";
            exit;
        }
Matheno
  • 4,112
  • 6
  • 36
  • 53
0

try replacing _DIR_ with dirname(__FILE__)

Also mysql is deprecated as the @jterry said.

Louay Alakkad
  • 7,132
  • 2
  • 22
  • 45