i have class Called Database here's the syntax
<?php
class Database
{
var $conn = null;
var $config = array(
'username' => 'root',
'password' => '',
'hostname' => 'localhost',
'database' => '<dbname here>'
);
function __construct() {
$this->connect();
echo 'constructor database';
}
function connect() {
if (is_null($this->conn)) {
$db = $this->config;
$this->conn = mysql_connect($db['hostname'], $db['username'], $db['password']);
if(!$this->conn) {
die("Cannot connect to database server");
}
if(!mysql_select_db($db['database'])) {
die("Cannot select database");
}
}
return $this->conn;
}
}
i've include this database file to another php class let say it's authenticate.php
include $_SERVER['DOCUMENT_ROOT'].'/<project_name>/configuration/database.php';
which the class is:
class authenticate {
public function __construct() {
$db = new Database();
$db->connect();
}
public function auth($username, $password) {
$password = $this->HASH($username, $password);
$query = "SELECT username, password FROM mt_user WHERE username = '$username' AND password='$password' LIMIT 1";
$row = mysql_query($query) or die(mysql_error());
$result = mysql_num_rows($row);
if ($result != 1) {
$status_login = "FALSE";
} else
if ($result == 1) {
$status_login = "TRUE";
}
mysql_close();
return $status_login;
}
public function getSalt($username) {
$query = "SELECT salt FROM mt_user WHERE username = '$username'";
$result = mysql_query($query);
$salt = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$salt = $row['salt'];
}
mysql_close();
return reset($salt);
}
public function HASH($username,$password){
$salt = $this->getSalt($username);
$output = sha1($salt.$password);
return $output;
}
}
?>
i call the function to some view called login.php but it's throwing error on authenticate.php that "Cannot select database" or Cannot Connect to DB.
What's wrong with this authenticate or database.php files?
thank you.