php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54941 mysql_select_db fails changing host
Submitted: 2011-05-27 14:57 UTC Modified: 2011-05-27 15:21 UTC
From: javier dot lanz at artica dot es Assigned:
Status: Not a bug Package: MySQL related
PHP Version: 5.3SVN-2011-05-27 (SVN) OS: Ubuntu 10.10
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: javier dot lanz at artica dot es
New email:
PHP Version: OS:

 

 [2011-05-27 14:57 UTC] javier dot lanz at artica dot es
Description:
------------
Hemos tenido un problema al intentar cambiar la conexión active entre dos bases de datos en distintos servidores para nuestro proyecto Pandora FMS.
Hemos hecho un test muy simple para intentar acotar el problema, y hemos descubierto que está en "mysql_select_db"
Creemos que puede ser porque las dos bases de datos se llaman igual, a pesar de estar en distintos servidores, pero no somos capaces de descubir por qué.
Para más información pónganse en conctacto con nosotros.

Hi there!

We've had a problem trying to change the active connection between two databases hosted in two different servers meanwhile working into our Pandora FMS project.

We've done a simple test trying to delimit the problem and we've discovered the problem is with "mysql_select_db" function.

We think maybe the problem could be because these two databases have the same name although being in different servers, but we're not able to know why.

If you need further information, please don't hesitate to contact me.

Regards

Javi & Miguel.

Test script:
---------------
<code><pre>
<?php
error_reporting(E_ALL);

$dbs = array(
	'miguel' => array('h' => '192.168.70.109', 'n' => 'pandora', 'p' => 'pandora', 'u' => 'root'),
	'sergio' => array('h' => '192.168.70.123', 'n' => 'pandora', 'p' => '6683', 'u' => 'root')
);

$dbconnections = array();
foreach ($dbs as $key => $db) {
	 $result = mysql_connect ($db['h'], $db['u'], $db['p']);
	 //var_dump($result);
	 
	 $dbconnections[$key] = $result;
}

var_dump($dbconnections);
echo "---BY DEFAULT---\n";
var_dump(mysql_get_host_info());

echo "---CHANGING HOST---\n";
var_dump(mysql_select_db('pandora', $dbconnections['miguel'])); //Fail changing to miguel server
var_dump(mysql_get_host_info()); //host info should display miguel's info, but sergio's is given

echo "-----QUERY---\n";
$consulta = mysql_query('use pandora;');
$consulta = mysql_query('SELECT nombre FROM tagente;');
var_dump(mysql_error());
var_dump(mysql_fetch_array($consulta));

echo "-----ANOTHER QUERY---\n";
$consulta = mysql_query('use pandora;', $dbconnections['miguel']);
$consulta = mysql_query('SELECT nombre FROM tagente;', $dbconnections['miguel']);
var_dump(mysql_error($dbconnections['miguel']));
var_dump(mysql_fetch_array($consulta));

?>
</pre>
</code>


Expected result:
----------------
array(2) {
  ["miguel"]=>
  resource(2) of type (mysql link)
  ["sergio"]=>
  resource(3) of type (mysql link)
}
---BY DEFAULT---
string(25) "192.168.70.123 via TCP/IP"
---CHANGING HOST---
bool(true)
string(25) "192.168.70.109 via TCP/IP" 
-----QUERY---
string(0) ""
array(2) {
  [0]=>
  string(7) "agente3"
  ["nombre"]=>
  string(7) "agente3"
}
-----ANOTHER QUERY---
string(0) ""
array(2) {
  [0]=>
  string(15) "miguel-portatil"
  ["nombre"]=>
  string(15) "miguel-portatil"
}


Actual result:
--------------
array(2) {
  ["miguel"]=>
  resource(2) of type (mysql link)
  ["sergio"]=>
  resource(3) of type (mysql link)
}
---BY DEFAULT---
string(25) "192.168.70.123 via TCP/IP"
---CHANGING HOST---
bool(true)
string(25) "192.168.70.123 via TCP/IP"
-----QUERY---
string(0) ""
array(2) {
  [0]=>
  string(7) "agente3"
  ["nombre"]=>
  string(7) "agente3"
}
-----ANOTHER QUERY---
string(0) ""
array(2) {
  [0]=>
  string(15) "miguel-portatil"
  ["nombre"]=>
  string(15) "miguel-portatil"
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-05-27 15:21 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2011-05-27 15:21 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

You should always pass the link identifier ($dbconnections[$key] in your case) as parameter. If that is not passed to mysql_* functions the last opened connection will be used. mysql_select_db()  changes the defalt database used on the specified connection. Has nothing to do with multiple connections.
 
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Sat Feb 04 16:05:48 2023 UTC