设为首页 加入收藏

TOP

连接到 MySQL 数据库
2011-06-12 21:11:59 来源: 作者: 【 】 浏览:562次 评论:0

连接到 MySQL 数据库

创建一个连接到MySQL数据库
在您可以存取数据库中的数据,您必须创建一个连接到数据库。

在PHP中,这一点与mysql_connect ( )函数。

语法

mysql_connect(servername,username,password);
ParameterDescription
servernameOptional. Specifies the server to connect to. Default value is "localhost:3306"
usernameOptional. Specifies the username to log in with. Default value is the name of the user that owns the server process
passwordOptional. Specifies the password to log in with. Default is ""
 

注:有更多的可用参数,但上面列出的是最重要的。访问我们充分的PHP MySQL的参考更详细的信息。

例如
在下面的例子中,我们存储连接在一个变量( $控制)以供日后使用的脚本。该“死”的一部分将被处死,如果连接失败:

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

关闭连接
连接将自动关闭时,脚本的目的。要关闭连接之前,请使用mysql_close ( )函数:

 

<?php$con = mysql_connect("localhost","peter","abc123");if (!$con)  {  die('Could not connect: ' . mysql_error());  }
// some code
mysql_close($con);?>
您看到此篇文章时的感受是:
Tags: 责任编辑:administrator
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到QQ空间
分享到: 
上一篇PHP的MySQL的创建数据库和表 下一篇什么是mysql?

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

相关栏目

最新文章

图片主题

热门文章

推荐文章

相关文章

广告位