现在的位置: 首页 > 综合 > 正文

php连接mysql

2013年12月02日 ⁄ 综合 ⁄ 共 511字 ⁄ 字号 评论关闭

PHP连接MySQL数据库是通过 mysql_connect() 函数来打开非持久的 MySQL 连接。

 

语法
mysql_connect(servername, username, password);

 

参数说明
servername:可选。要连接的服务器名称,默认是 "localhost:3306",一般填写 localhost 即可。
username:可选。登录数据库服务器的用户名,一般都是root。
password:可选。登录数据库服务器的密码。

 

例子

 

<?php
header("Content-type: text/html; charset=utf-8");
$link_id = @mysql_connect('localhost', 'root', '123456789');
if (!$link_id) {
    die('连接服务器失败');
}
if (!@mysql_select_db('web', $link_id)) {
    die('连接数据库失败');
}
if (!@mysql_query("set names 'utf8'", $link_id)) {
    die('设置utf8格式失败');
}

mysql_close();
?>
文章 摘自  php连接mysql

抱歉!评论已关闭.