4

I want to load a configuration file in a class. Here is the content of config.php

<?php
$__error_reporting_level=1;
$server='localhost';
$user='root';
$password='';
$dbase='eauction';
?>

Content of sql.php

<?php
include ('config.php');
error_reporting($__error_reporting_level);

class sql
{
 function connect()
 {
  $connections = mysql_connect($server, $user,$password) or die ('Unabale to connect to the database');
  mysql_select_db($dbase) or die ('Unable to select database!');
  return;
 }

 function login($email, $pwd)
 {
  $this->connect();
  $result = $this->qry("SELECT uid,nameF FROM user WHERE email='".$email."' AND password='".$pwd."'");
  $row=mysql_fetch_array($result);
  if (mysql_num_rows($result)>0)
   return array($row[0],$row[1]);
  else
   return array(0,0);
 }
}
?>

I execute the code using

include ('core/sql.php');
$obj = new sql;
$data=$obj->login($email,$pwd);
print_r($data);

I get this error

Unable to select database!

Ignore mysql injection issue, I just need to execute the code perfectly

4
  • You can't use $server,$user etc in class functions directly . Commented Feb 28, 2012 at 3:51
  • make sure database name is correct . Commented Feb 28, 2012 at 3:52
  • Database name is correct bro ! Commented Feb 28, 2012 at 3:53
  • why are you all still creating the "database classes" instead of using PDO or MySQLi ( if you live in mysql-only realm ) ? Commented Feb 29, 2012 at 2:17

7 Answers 7

11

Why not use a .ini file?

config.ini

server=test
user=root
pass=pass
dbname=mydb

and with in your class have something like

class A {

    public $config;

    public function __construct() {
       $this->config = parse_ini_file('config.ini', true);
    }

    public function sql() {
        $connections = mysql_connect($this->config['server'], $this->config['user'],$this->config['password']) or die ('Unabale to connect to the database');
  mysql_select_db($this->config['dbase']) or die ('Unable to select database!');
  return;
    }
}

just another way of doing it, make sure your database is correctly named too.

Additionally, if you want to use your current config.php then you will need to include in the method you are using the variables in. It cannot be used from outside that scope.

function connect()
 {
    include ('config.php');
  $connections = mysql_connect($server, $user,$password) or die ('Unabale to connect to the database');
  mysql_select_db($dbase) or die ('Unable to select database!');
  return;
 }
Sign up to request clarification or add additional context in comments.

Comments

6

Read up on variable scope in the PHP manual.

You've included the file before the class declaration, at global scope, not accessible to the class method scope. If you want to use those variables inside class methods, you'll need to either access them globally via $GLOBALS[] or the global keyword, or better yet, pass them into the function that uses them.

include ('config.php');
// Now all your variables are defined at global scope!!!
error_reporting($__error_reporting_level);

class sql
{
 // Pass as params to the function
 function connect($server, $user, $password, $dbase)
 {
  $connections = mysql_connect($server, $user,$password) or die ('Unabale to connect to the database');
  mysql_select_db($dbase) or die ('Unable to select database!');
  return;
 }
 // etc...
 // etc...
}

You might also consider setting them up as class properties, and passing them in the constructor:

class sql
{
  public $server;
  public $user;
  public $password;
  public $dbase;

  public function __construct($server, $user, $password, $dbase) {
    $this->server = $server;
    $this->user = $user;
    // etc...
    $connections = mysql_connect($this->server, $this->user, $this->password);
  }
}

Comments

1

use defined variables in your config.

http://php.net/manual/en/function.define.php

Comments

1

try this

config.ini
[database]
username = root
password = 1
dbname = users
dsn = localhost
......
class Database{
public $koneksi;
  function __construct(){
    try{
        $config = parse_ini_file('config.ini'); 
        $this->koneksi = new PDO("mysql:host=" . $config['dsn'] . ";dbname=" . $config['dbname'], $config['username'],$config['password'],array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
        $this->koneksi->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            }catch(PDOException $e){
        $e->getMessage();
    }
  }
}

Comments

0

can do it this way.. the issue on your code must be that the variable you declare in config.php is not accessible. you can make it global or try this code of mine.

<?php
class ConnectionSettings {
    private $hostname = 'localhost';
    private $username = 'root';
    private $password = '';
    private $db = 'cordiac_db';
    protected $connLink;

    // protected 'connect()' method
    protected function connect(){

        // establish connection
        if(!$this->connLink = mysql_connect($this->hostname, $this->username, $this->password)) {
            throw new Exception('Error connecting to MySQL: '.mysql_error());
        }

        // select database
        if(!mysql_select_db($this->db, $this->connLink)) { 
            throw new Exception('Error selecting database: '.mysql_error());
        }
    }
}

Comments

0

Try this

config.php
define('SERVER',$server);
define('USER',$user);
define('PASSWORD',$password);
define('DBASE',$dbase); 



----------class----------     
<?php
include ('config.php');
error_reporting($__error_reporting_level);

class sql
{
 protected  $server=SERVER;
 protected $user=USER;
 protected $password=PASSWORD;
 protected $dbase=DBASE;
 private function connect()
 {
  $connections = mysql_connect($this->server, $this->user,$this->password) or die ('Unabale to connect to the database');
  mysql_select_db($this->dbase) or die ('Unable to select database!');
  return;
 }
........
........

1 Comment

I want to save and load mysql_connect parameter from the config.php so I dont need to update this !
0

You need to load the config file in the same scope that you want to use the variables:

class sql
{
    function connect()
    {
        include 'config.php';
        $connections = mysql_connect($server, $user,$password) or die ('Unabale to connect to the database');
        mysql_select_db($dbase) or die ('Unable to select database!');
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.