Grammar
Mysql_connect (Servername,username,password);
Example
In the following example, we store a connection in a variable ($con) in a script for later use. If the connection fails, the "Die" section is executed:
| The code is as follows |
Copy Code |
<?php $con = mysql_connect ("localhost", "Peter", "abc123"); if (! $con) { Die (' Could not connect: '. Mysql_error ()); } Some code ?> |
The above is connected to the local database, the following localhost to the remote IP can be
Instance
| code is as follows |
copy code |
| $conn = mysql_connect (' 150.125.221.25 ', ' root ', ' 123 '); if (! $conn) echo "Failed!"; Else echo "Success!"; /SQL statement to extract information from table $sql = "SELECT * from user where username= ' $user _ Name ' "; //Execute SQL query $result =mysql_db_query (' info ', $sql, $conn); //Get query Results $row =mysql_fetch_row ($result); mysql_close (); |