About new_link parameters of mysql_connect

Source: Internet
Author: User

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 ?)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.