strange problem with 2 mysql db. and 2 clases.
Posted: Tue Aug 05, 2008 1:14 pm
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
db.class.php
and same for login_db.class.php
db.class.php
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?
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>
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);
}
}
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);
}
}
any Ideas why is this happening?