Assume that there are two databases test1 and test2 on 127.0.0.1. The test1 database contains the test1 table and test2 contains the test2 table.
$ Servername = "127.0.0.1 ";
$ Username = "root ";
$ Password = "root ";
$ Conn1 = mysql_connect ($ servername, $ username, $ password );
Mysql_select_db ('test1', $ conn1 );
$ Conn2 = mysql_connect ($ servername, $ username, $ password );
Mysql_select_db ('test2', $ conn2 );
// The following error is returned: Table 'test2. test1 'doesn' t exist. Because $ conn2 has the same connection parameter as $ conn1, $ conn2 does not open a new connection, but returns $ conn1, therefore, when executing the following code, the database has been switched to test2. If you do not want to report an error, change the code in the second sentence to $ conn2 = mysql_connect ($ servername, $ username, $ password, true );
$ Dt = mysql_query ('select * from test1', $ conn1 );
If (false = $ dt)
{
Echo mysql_error ($ conn1 );
}
While ($ row = mysql_fetch_assoc ($ dt )){
Echo var_dump ($ row );
}
Save the following code as http: // localhost/mysql. php, visit this page, and use the Navicat server to monitor the database connection of localhost before the page ends.
$ Servername = "127.0.0.1 ";
$ Username = "root ";
$ Password = "root ";
$ Conn1 = mysql_connect ($ servername, $ username, $ password );
$ Conn2 = mysql_connect ($ servername, $ username, $ password );
Sleep (10 );
Only one connection is found in the Process List,
If you change the code in line 1 to $ conn2 = mysql_connect ($ servername, $ username, $ password, true), you can see two connections in the process list.
If you change the code of Line 1 to $ conn1 = mysql_connect ($ servername, $ username, $ password, true), visit the page, it is found that there is only one connection in the Process List (assign a new resource ID to a resource variable, will the previous resource be automatically released ?)