简体   繁体   English

无法与mysql连接

[英]Not connect with mysql

I have 2 connection string in 2 file connect.php connect to 2 mysql database on 2 server. 我在2个文件中有2个连接字符串connect.php连接到2个服务器上的2个mysql数据库。

File dbconnect1: 文件dbconnect1:

<?php
$conn = mysql_connect('sv1','root','123456') or dir("No connect");
mysql_select_db('db1')or dir("not connect database");
mysql_query("SET charactor_set_results=utf8",$conn);
mysql_query("SET NAMES 'utf8'");
?>

File dbconnect2: 文件dbconnect2:

<?php
$conn = mysql_connect('sv1','root','123456') or dir("No connect");
mysql_select_db('db2')or dir("not connect database");
mysql_query("SET charactor_set_results=utf8",$conn);
mysql_query("SET NAMES 'utf8'");
?>

When I include in file php and exercute query it not show result. 当我包含在文件php中并执行查询时,它不会显示结果。 One of 2 connection string not work. 2个连接字符串之一不起作用。 Why? 为什么?

I think it is because you use the same variable $conn for both DB connections. 我认为这是因为两个数据库连接都使用了相同的变量$ conn。 Replace it with $conn2 for example in the dbconnect2.php file. 例如,在dbconnect2.php文件中将其替换为$ conn2。 At the moment your first connection get overwritten by the second connection. 目前,您的第一个连接被第二个连接覆盖。 This is because you do them one after another. 这是因为您一个接一个地做它们。

Plus you have to put TRUE for the fourth paramether in mysql_connect() function in order to start a new connection to the same DB server. 另外,您必须在mysql_connect()函数的第四个参数中输入TRUE,以启动与同一数据库服务器的新连接。 http://php.net/manual/en/function.mysql-connect.php http://php.net/manual/zh/function.mysql-connect.php

File dbconnect2: 文件dbconnect2:

<?php
$conn2 = mysql_connect('sv1','root','123456', TRUE) or die("No connect");
mysql_select_db('db2', $conn2)or die("not connect database");
mysql_query("SET charactor_set_results=utf8", $conn2);
mysql_query("SET NAMES 'utf8'", $conn2);
?>

Plus you have to point which MySQL connection you want to use in mysql_select_db() function(s) and mysql_query() function(s). 另外,您必须指出要在mysql_select_db()函数和mysql_query()函数中使用哪个MySQL连接。

You have to change the second db connection variable say $conn2 and also use the connection statement as follows: 您必须更改第二个数据库连接变量,例如$ conn2,并按如下所示使用连接语句:

$conn2 = mysql_connect('sv1','root','123456', TRUE) or die("No connect");

Where the 4th parameter has to pass as TRUE. 其中第四个参数必须作为TRUE传递。

Reference: http://php.net/manual/en/function.mysql-connect.php 参考: http : //php.net/manual/en/function.mysql-connect.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM