1

I just tried to connect a secondary database like this example bellow but i don't know why refuse to work. Any idea?

I mention that each database connection works properly individualy.

$db_HOST = "localhost";
$db_USER = "db_user";
$db_PASS = "db_pass";
$db_NAME1 = "db_test1";
$db_NAME2= "db_test2";

$db_LINK1 = mysql_connect($db_HOST, $db_USER, $db_PASS) or die("Technical revision. Please try again later!");
mysql_select_db($db_NAME1, $db_LINK1) or die("Couldn't select database");

$db_LINK2 = mysql_connect($db_HOST, $db_USER, $db_PASS, true) or die("Technical revision. Please try again later!");
mysql_select_db($db_NAME2, $db_LINK2) or die("Couldn't select database");

Errors i get in the log file:

PHP Warning:  mysql_fetch_array() expects parameter 1 to be resource, boolean given in /config/global/variables.php on line 27

PHP Warning:  mysql_fetch_array() expects parameter 1 to be resource, boolean given in config/global/hello.php on line 3

Thank you!

Adrian
  • 159
  • 7

3 Answers3

0

You should really use PDO but your code works if you use your link in your db select. For example:

$db1 = mysql_connect($hostname, $username, $password); 
$db2 = mysql_connect($hostname, $username, $password, true); 

mysql_select_db('database1', $db1);
mysql_select_db('database2', $db2);

that should work. You forgot to select which database should be used on every link.

René Höhle
  • 26,716
  • 22
  • 73
  • 82
0

Do the following (with PDO instead of mysql_connect as the latter is deprecated):

$db_HOST = "localhost";
$db_USER = "db_user";
$db_PASS = "db_pass";
$db_NAME1 = "db_test1";
$db_NAME2= "db_test2";

try {
    $db_LINK1 = new PDO('mysql:host='.$db_HOST.';dbname='.$db_NAME1, $db_USER, $db_PASS);
    foreach($db_LINK1->query('SELECT * from FOO') as $row) {
        print_r($row);
    }
    $dbh = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

try {
    $db_LINK2 = new PDO('mysql:host='.$db_HOST.';dbname='.$db_NAME2, $db_USER, $db_PASS);
    foreach($db_LINK2->query('SELECT * from FOO') as $row) {
        print_r($row);
    }
    $dbh = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

More info here: php.net/manual/en/pdo.connections.php

If the second connection fails check the exact error message.

Blackbam
  • 17,496
  • 26
  • 97
  • 150
0

I thnk I found the problem. I forgot to change all queries accordind to the new multiple connection. LE: Solve confirmed! Thank you all!

Adrian
  • 159
  • 7