سؤال

I have the following code for test, and I just found the 2nd parameter is not actually working.

$conn1 = mysql_connect("127.0.0.1", "xxxx", "xxxx");
$conn2 = mysql_connect("127.0.0.1", "xxxx", "xxxx");

mysql_select_db("test", $conn1);
mysql_select_db("yangshengfun", $conn2);

if (!$res = mysql_query("select * from proxy_ips limit 1", $conn1)) {
    echo mysql_error($conn1);
}


if (!$res = mysql_query("select * from wp_posts limit 1", $conn2)) {
    echo mysql_error($conn2);

The tables in database 'test' and 'yangshengfun' are complately different. An error occured while I run this code:

Table 'yangshengfun.proxy_ips' doesn't exist

Seems when I call mysql_select_db for $conn2, it changes the current db of $conn1 too, any ideas?

هل كانت مفيدة؟

المحلول

try this

 $conn1= mysql_connect("host_name", "user_name", "pass_word") or die('not connected'); 
 mysql_select_db("database_name", $conn1);

Here the "die($message)" function prints a message if mysql_connect() function can't connect with db.

نصائح أخرى

try this

$conn1 = mysql_connect("127.0.0.1", "xxxx", "xxxx", true);
$conn2 = mysql_connect("127.0.0.1", "xxxx", "xxxx", true);

Note : mysql_* is deprecated. use mysqli_* or pdo

Use mysqli instead:

<?php

    $conn1 = new mysqli(host, user, password, db);

    $conn2 = new mysqli(host2, user2, password2, db2);

?>

From the documentation of mysql_connect() of the PHP Manual

If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters. In SQL safe mode, this parameter is ignored.


This (mysql_*) extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, Prepared Statements of MySQLi or PDO_MySQL extension should be used to ward off SQL Injection attacks !

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top