php - Using multiple mysql connections in 1 class -
i writing cronjob transfer data 1 table, process , apply web application. have 2 seperate database connections this. @ moment, connection credentials same except it's different databases, going live, entirely seperated.
the issue creating connection assigning in class $this->staging , other called $this->production. use mysql_ping ensure both connections working.
what appears happen when query, disregards connection identifier ($this->staging example) , tries query on last of 2 connections created.
i use connection identifier within query:
// connection: $this->staging = $this->mysql($config['staging']); $this->production = $this->mysql($config['production']); $sql = "select * table"; $query = mysql_query($sql,$this->staging); // returns unknown table in database defined in $this->production. i return mysql_connect $this->mysql() method, , not mysql_select_db(). if try returning mysql_select_db() mysql_ping not work.
here mysql() method:
// connect(): connect database (mysql): private function mysql($cred) { $connect = mysql_connect($cred['server'],$cred['username'],$cred['password']); if($connect) { $db = mysql_select_db($cred['database']); if($db) { return $connect; } else { $this->informer("[fatal]","could not make mysql database connection on server ".$_server['server_addr']." database: ".$cred['database']); } } else { $this->informer("[fatal]","the database credentials appears wrong. ".$_server['server_addr']." mysql database: ".$cred['database']); } }
the problem happens because connecting same mysql server, when connect staging (second connection) php returns same connection created production , don't create new one, force php create new 1 should change "mysql_connect" call this:
$connect = mysql_connect($cred['server'],$cred['username'],$cred['password'], true); this force php start new connection.
Comments
Post a Comment