conn.php文件://连接数据库文件
mysql_connect('localhost','username','password') or die ('数据库连接失败');
mysql_select_db('数据库名') or die ('数据库连接失败');//mysql里的一个数据库
mysql_query("set names 'utf8'");//设置编码,也可以为GBK等
?>
这样就连上数据库了,你用浏览器打开此页面,是不显示内容的,说明已经连上。
用下面例子测试一下,假使你的数据库下有张表叫user,并且里面有数据:
test.php文件://工作文件
include('conn.php');
$sql="select * from user";
$query=mysql_query($sql);
$result=mysql_fetch_array($query);
var_dump($result);
?>
运行test.php文件即可
你已经有连接的代码 然后运行这个index.php
文件 就连接上了