mysql选择db-phpmysql_select_db参数2不起作用


mysql select db - php mysql_select_db parameter 2 not 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);

数据库"test"answers"yangshengfun"中的表完全不同。运行此代码时发生错误:

Table 'yangshengfun.proxy_ips' doesn't exist

似乎当我为$conn2调用mysql_select_db时,它也更改了$conn1的当前数据库,有什么想法吗?

尝试这个

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

如果mysql_connect()函数不能与db连接,那么"die($message)"函数会打印一条消息。

尝试这个

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

注意:mysql_*已弃用。使用mysqli_*pdo

改为使用mysqli:

<?php
    $conn1 = new mysqli(host, user, password, db);
    $conn2 = new mysqli(host2, user2, password2, db2);
?>

来自PHP手册的mysql_connect()文档

如果使用相同的参数对mysql_connect()进行第二次调用,不会建立新的链接,而是将返回已打开的链接new_link参数修改此行为并使mysql_connect()始终打开一个新的链接,即使之前使用相同的参数。在SQL安全模式下,此参数将被忽略。


PHP 5.5.0起,此(mysql_*)扩展已弃用,将来将删除。相反,应该使用MySQLiPDO_MySQL扩展的Prepared Statements来抵御SQL注入攻击!