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
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());
}
}
}