php - Using external variables in classes -


i have 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 try create instance of $db class in index.php, errors (cannot obtain defined variables in class)

having directory structure:

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

will force use: include('../config/config.php'); or define absolute path goes root , do: include(path.'config/config.php');.

your include "config/config.php"; interpreted include('root/classes/config/config.php') which, assume, doesn't exists.

you should anyway pass, samy suggested, constants class via constructor can use same class multiple connection different database variables.

and also, not suggested use mysql or mysqli function anymore. learn pdo.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -