-2

I have a directory structure:

app
cofig
 config.php
classes
 db.class.php
index.php 

config.php:

<?php

define('DB_HOST', 'localhost'); 
define('DB_USER', 'root'); 
define('DB_PASS', ''); 
define('DB_NAME', 'db_name');  

db.class.php:

<?php 
include "config/config.php";

class DB {

private $conn;
private $results;

public function __construct() {

$this->conn = mysql_connect(DB_HOST, DB_USER, DB_PASS);//**This doesn't work**
mysql_select_db(DB_NAME, $this->conn);
}

}
?>

when I try to create an instance of the $db class in index.php, I get errors (cannot obtain DEFINED variables in class)

mpet
  • 983
  • 4
  • 15
  • 33

4 Answers4

3

Having this directory structure:

app
config
 config.php
classes
 db.class.php
index.php 

will force you to use: include('../config/config.php'); or define an absolute PATH that goes to the root and do: include(PATH.'config/config.php');.

Your include "config/config.php"; is interpreted as include('root/classes/config/config.php') which, I assume, doesn't exists.

You should anyway pass, as Samy suggested, constants to the class via constructor so that you can use the same class for multiple connection with different database variables.

And also, it is not suggested to use mysql or mysqli function anymore. Learn about PDO.

Shoe
  • 74,840
  • 36
  • 166
  • 272
  • You're right, but including isn't the fix, I still cannot get defined DB_ variables from the db class – mpet Jun 05 '12 at 17:44
2

Assuming that "cofig" in your directory structure is a typo, use:

include("../config/config.php");
Scott Saunders
  • 29,840
  • 14
  • 57
  • 64
2

According to your directory structure include path should be '../config/config.php'. Since you are using include, it does not throw fatal error and continue code execution whther file is included or not. Here your file is not included as per the code. Try this

  <?php 
include "../config/config.php";

class DB {

    private $conn;
    private $results;

    public function __construct() {

        $this->conn = mysql_connect(DB_HOST, DB_USER, DB_PASS);//**This doesn't work**
        mysql_select_db(DB_NAME, $this->conn);
     }

 }
?>
Nishu Tayal
  • 20,106
  • 8
  • 49
  • 101
1

Pass these variables to your class as parameters, it's cleaner anyway.

class DB {
    public function __construct($host, $dbName, $user, $password) {
        $this->conn = mysql_connect($host, $user, $password);
        mysql_select_db($dbName, $this->conn);
    }
}

new DB(DB_HOST, DB_NAME, DB_USER, DB_PASS);

By the way, mysql_ functions are in the process of being deprecated.

Samy Dindane
  • 17,900
  • 3
  • 40
  • 50
  • 1
    "By the way, mysql_ functions are deprecated." Useful comment – mpet Jun 05 '12 at 17:36
  • What do you recommend instead? PDO.. or..? – mpet Jun 05 '12 at 17:37
  • PDO or mysqli, though [PDO is probably better.](http://stackoverflow.com/questions/10696696/which-one-is-faster-and-lighter-mysqli-pdo) – Jeroen Jun 05 '12 at 17:39
  • PHP recommend *mysqli* or PDO (http://www.php.net/manual/en/intro.mysql.php). -- I'd say PDO... or an ORM. :) – Samy Dindane Jun 05 '12 at 17:39
  • Did you mean for this function in the first place? http://php.net/manual/en/function.mysql-db-query.php it has been deprecated as of 5.3.0. I think that mysql_connect, mysql_query still exist – mpet Jun 05 '12 at 17:40