?>
php连接mysql数据库:
<?php
$db_host = 'localhost';
$db_database = 'hilo8';
$db_username = 'root';
$db_password = '123456';
$connection=mysql_connect($db_host,$db_username,$db_password);//连接到数据库
mysql_query("set names 'gb2312'");//编码转化
if(!$connection){
die("could not connect to the database:</br>".mysql_error());//诊断连接错误
}
$db_selecct=mysql_select_db($db_database); //选择数据库
if(!$db_selecct)
{
die("could not to the database</br>".mysql_error());
}
$result=mysql_query("select * from hi_yuedu");//执行查询
if(!$result){
die("could not to the database</br>".mysql_error());
}
// array mysql_fetch_row(resource $result);
while($result_row=mysql_fetch_row(($result)))//取出结果并显示
{
$num=$result_row[0];
$age=$result_row[1];
$name=$result_row[2];
echo "$num /";
echo "$age /";
echo "$name";
}
mysql_close($connection);//关闭连接
?>
说明:连接access数据库时默认编码为gb2312,在实际应用中要注意这一点,否则会出现读出中文数据是乱码。
上一篇:PHP变量$_SERVER获取客户端、服务器相关信息
下一篇:PHP经验-请手动释放你的资源(Please release resources manually)
讨论数量:0