Php connects to a remote mysql database instance. 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 _ in php, if we want to connect to a remote database, the connection 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, run the "die" section:
The code is as follows: |
|
$ Con = mysql_connect ("localhost", "peter", "abc123 "); If (! $ Con) { Die ('could not connect: '. mysql_error ()); } // Some code ?> |
The above is to connect to the local database, and then change localhost to a remote IP address.
Instance
The code is as follows: |
|
$ Conn = mysql_connect ('192. 125.221.25 ', 'root', '123 '); If (! $ Conn) echo "failed! "; Else echo "successful! "; // SQL statement used to extract 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 (); |
Bytes. Syntax: mysql _...