Page 1 of 1

Database connection

Posted: Fri May 11, 2007 5:30 am
by winsonlee
database.php

Code: Select all

class MySQLDB
  {
    var $connection;

    function MySQLDB(){
      $this->connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS) or die ("Could not connect to MySQL");
      mysql_select_db("db1", $this->connection) or die(mysql_error());
    }

    function query($query){
  		$result = mysql_query($query, $this->connection);
  		if (!$result) {
         die('Invalid query: ' . mysql_error());
      }else
			  return $result;

    }
}		
 class MySQLDB2
  {
    var $connection;

    function MySQLDB2(){
      $this->connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS) or die ("Could not connect to MySQL");
      mysql_select_db("db2", $this->connection) or die(mysql_error());
    }

    function query($query){
  		$result = mysql_query($query, $this->connection);
  		if (!$result) {
         die('Invalid query: ' . mysql_error());
      }else
			  return $result;

    }
}

  $database =  new MySQLDB;
  $database2 =  new MySQLDB2;
I have two classes that connects to two different database.
I face some problem when i tried to create two object at the same time.

Should I create the object only when I wants to make a query to the database and close it after the query ?

I have a feeling mysql does not allow me to open two or more connection simultaniously.

Posted: Fri May 11, 2007 5:42 am
by Oren
I didn't read your code, but check "mysql.max_links" using phpinfo().

Posted: Fri May 11, 2007 6:02 am
by winsonlee
With my current way of coding what i face is after a while the object for the database connection got destroyed and it returns invalid query.

mysql
MySQL Support enabled
Active Persistent Links 0
Active Links 0
Client API version 5.0.21

Directive Local Value Master Value
mysql.allow_persistent On On
mysql.connect_timeout 60 60
mysql.default_host no value no value
mysql.default_password no value no value
mysql.default_port no value no value
mysql.default_socket no value no value
mysql.default_user no value no value
mysql.max_links Unlimited Unlimited
mysql.max_persistent Unlimited Unlimited
mysql.trace_mode Off Off

mysqli
MysqlI Support enabled
Client API library version 5.0.21
Client API header version 5.0.21
MYSQLI_SOCKET /tmp/mysql.sock

Directive Local Value Master Value
mysqli.default_host no value no value
mysqli.default_port 3306 3306
mysqli.default_pw no value no value
mysqli.default_socket no value no value
mysqli.default_user no value no value
mysqli.max_links Unlimited Unlimited
mysqli.reconnect Off Off

odbc
ODBC Support enabled
Active Persistent Links 0
Active Links 0
ODBC library Win32

Directive Local Value Master Value
odbc.allow_persistent On On
odbc.check_persistent On On
odbc.default_db no value no value
odbc.default_pw no value no value
odbc.default_user no value no value
odbc.defaultbinmode return as is return as is
odbc.defaultlrl return up to 4096 bytes return up to 4096 bytes
odbc.max_links Unlimited Unlimited
odbc.max_persistent Unlimited Unlimited