If we want to connect to a remote database in php, the connection method is also very simple. We only need to change the local connection localhost or 127.0.0.1 to specify an IP address of the remote server.
Grammar
mysql_connect(servername,username,password);
Example
In the example below, we store the connection in a variable ($con) for later use in the script. If the connection fails, the "die" part will be executed:
The code is as follows
代码如下 |
复制代码 |
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// some code
?>
|
|
Copy code
|
|
代码如下 |
复制代码 |
$conn=mysql_connect('150.125.221.25','root','123');
if(!$conn) echo "失败!";
else echo "成功!";
// 从表中提取信息的sql语句
$sql="SELECT * FROM user where userName='$user_name'";
// 执行sql查询
$result=mysql_db_query('info', $sql, $conn);
// 获取查询结果
$row=mysql_fetch_row($result);
mysql_close();
|
$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, now just change the localhost to the remote IP
Example
The 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 the 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();
http://www.bkjia.com/PHPjc/630686.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/630686.htmlTechArticleIf we want to connect to a remote database in php, the connection method is also very simple. We only need to connect the local host to localhost or 127.0. 0.1 can be changed to specify an IP address of the remote server. Syntax mysql_...
|
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn