Page 1 of 1

strange problem with 2 mysql db. and 2 clases.

Posted: Tue Aug 05, 2008 1:14 pm
by freeAngel_007
ok I have 2 databases called login_db and db

i have main index.php file called as first then I have 2 clases in 2 different files (I use them to connect db).

index.php

Code: Select all

 
<html>
<head></head>
 
<body>
<pre>
<?php
require_once('login_db.class.php');
require_once('db.class.php');
 
class test extends db{
    
    public function __construct()
    {
    $obj = new login_db;
    echo "this is test for db login_db\n";
    
    echo "query: ";
    
    echo $query = "SELECT * FROM users";
    echo "\n";
    
    print_r($obj->CallQuery($query));
 
    echo "\nthis is test for db\n";
    
    echo "poziadavka: ";
    
    echo $query = "SELECT * FROM users";
    echo "\n";
    
    print_r($this->CallQuery($query));
        
    echo "\n exit";
 
    }
}
 
 
 
new test();
 
exit;
?>
</pre>
</body>
</html>
 
db.class.php

Code: Select all

 
class db {
    private $connection;
    
    
    private $hostName = "localhost";
    private $userName = "username";
    private $password = "password";
    private $databaseName = "db";
 
    public function __construct() {
        session_start();
        self::connect();
    }
 
    public function connect() {
        $connection = $this->createDBConnection();
        $this->showDBconnection($connection);
        $this->selectDB($connection);
        $this->query("SET NAMES utf8");
        return $connection;     
        
    }
 
    protected function createDBConnection() {
        if (!( mysql_connect($this->hostName, $this->userName, $this->password))) {
            die("error");           
        }
        else {
            return mysql_connect($this->hostName, $this->userName, $this->password);
        }
        
    }
    protected function selectDB($connection) {
        if (!mysql_select_db($this->databaseName, $connection)) {
            die('error select db'); 
        }
    }
    
    private function showDBconnection($connection) {
        $this->connection = $connection;
    }
 
    public function CallQuery($query) 
    {
        if (!(@ mysql_query($query, $this->connection))) {
            die("error query " . $query);
        }
        else {
            $result = @ mysql_query($query, $this->connection);
            
            return @ mysql_fetch_assoc($result);
        }
        
    }
 
and same for login_db.class.php
db.class.php

Code: Select all

 
class login_db {
    private $connection;
    
    
    private $hostName = "localhost";
    private $userName = "username";
    private $password = "password";
    private $databaseName = "login_db";
 
    public function __construct() {
        session_start();
        self::connect();
    }
 
    public function connect() {
        $connection = $this->createDBConnection();
        $this->showDBconnection($connection);
        $this->selectDB($connection);
        $this->query("SET NAMES utf8");
        return $connection;     
        
    }
 
    protected function createDBConnection() {
        if (!( mysql_connect($this->hostName, $this->userName, $this->password))) {
            die("error");           
        }
        else {
            return mysql_connect($this->hostName, $this->userName, $this->password);
        }
        
    }
    protected function selectDB($connection) {
        if (!mysql_select_db($this->databaseName, $connection)) {
            die('error select db'); 
        }
    }
    
    private function showDBconnection($connection) {
        $this->connection = $connection;
    }
 
    public function CallQuery($query) 
    {
        if (!(@ mysql_query($query, $this->connection))) {
            die("error query " . $query);
        }
        else {
            $result = @ mysql_query($query, $this->connection);
            
            return @ mysql_fetch_assoc($result);
        }
        
    }
 
and what happend when I run it on server. I will get result from login_db but error query from db class.

any Ideas why is this happening?

Re: strange problem with 2 mysql db. and 2 clases.

Posted: Tue Aug 05, 2008 1:19 pm
by pkbruker
Could the post the error you get?

Re: strange problem with 2 mysql db. and 2 clases.

Posted: Wed Aug 06, 2008 2:15 pm
by freeAngel_007
i get only error from mysql as query error.

but query is 100% ok