Home  >  Article  >  php教程  >  php连接远程mysql数据库实例

php连接远程mysql数据库实例

WBOY
WBOYOriginal
2016-05-25 16:37:481773browse

在php中如果我们要连接远程数据库连接方法也很简单,我们只要把本地连接localhost或127.0.0.1改成指定远程服务器一IP地址即可.

语法:mysql_connect(servername,username,password);

例子:在下面的例子中,我们在一个变量中 ($con) 存放了在脚本中供稍后使用的连接,如果连接失败,将执行 "die" 部分,代码如下:

<?php 
$con = mysql_connect("localhost","peter","abc123"); 
if (!$con) 
{ 
  die(&#39;Could not connect: &#39; . mysql_error()); 
} 
 
// some code

上面是连接本地数据库,下面把localhost改成远程IP即可了,实例代码如下:

$conn=mysql_connect(&#39;150.125.221.25&#39;,&#39;root&#39;,&#39;123&#39;); 
if(!$conn) echo "失败!"; 
else echo "成功!"; 
 //开源代码phprm.com 
 // 从表中提取信息的sql语句 
$sql="SELECT * FROM user where userName=&#39;$user_name&#39;"; 
// 执行sql查询 
$result=mysql_db_query(&#39;info&#39;, $sql, $conn); 
// 获取查询结果 
$row=mysql_fetch_row($result); 
 mysql_close();


教程网址:

欢迎收藏∩_∩但请保留本文链接。

Statement:
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