Connect php to a remote mysql database instance

Source: Internet
Author: User
In php, if we want to connect to a remote database, the method is also very simple. we just need to change the local connection localhost or 127.0.0.1 to a specified remote server IP address. syntax: mysql_connect (servernam... in php, if we want to connect to a remote database, the method is also very simple. we just need to change the local connection localhost or 127.0.0.1 to a specified remote server IP address.

Syntax: mysql_connect (servername, username, password );

Example: In the following example, we store the connection in a variable ($ con) in the script for later use. if the connection fails, the "die" part will be executed, the code is as follows:

 

The above is to connect to the local database. now you can change localhost to a remote IP address. the instance code is as follows:

$ Conn = mysql_connect ('192. 125.221.25 ', 'root', '123'); if (! $ Conn) echo "failed! "; Else echo" successful! "; // Open-source code phprm.com // SQL statement for extracting information FROM the table $ SQL =" SELECT * FROM user where userName = '$ user_name '"; // execute SQL query $ result = mysql_db_query ('info', $ SQL, $ conn); // Obtain the query result $ row = mysql_fetch_row ($ result); mysql_close ();


Tutorial URL:

You are welcome to add your _ favorites to the Favorites folder, but please keep the link for this article.

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.