1

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.

randytan
  • 1,029
  • 5
  • 23
  • 52
  • include should use filesystem paths, not URLs, if you want to actually include the code – Mark Baker Aug 04 '13 at 11:30
  • 1
    Have you verified the value of `$db['database']` at the time you call `mysql_select_db` (which is deprecated, by the way)? – GolezTrol Aug 04 '13 at 11:31
  • @GolezTrol: yes i've check it. It's setup the right db. – randytan Aug 04 '13 at 11:33
  • This is not an answer but may help. The usage of the `var` keyword is deprecated. (It's the PHP4 way, didn't see this since years).. Use `private`, `protected` or `public` instead.. – hek2mgl Aug 04 '13 at 11:45
  • @hek2mgl thanks for your comment. i've changed it to public instead. I think this is simple problem. But i couldn't figure it out. – randytan Aug 04 '13 at 11:49
  • http://stackoverflow.com/q/12859942/2536029 – mnagel Aug 04 '13 at 12:16

1 Answers1

0

tell mysql which resource to use

    if(!mysql_select_db($db['database'],$this->conn)) {
        die("Cannot select database");

also mysql_* is deprecated, before you go much further consider switching to mysqli_* or PDO

exussum
  • 18,275
  • 8
  • 32
  • 65
  • the error function is not in database class, it's on the authenticate class. I've found that the class cannot calling connect function in database class. how to call the function in authenticate class? is the constructor i made is correct? – randytan Aug 05 '13 at 03:23