下面是一段简单的php连接数据库并打其中一张表的数据信息,文件名index.php,
博主在本地测试
数据库地址:localhost
用户名:root
密码:空
数据库名:saixinjituan
数据库表可以在文末下载
代码如下:
<?php
//(1)数据库配置信息
$db_host = "localhost";
$db_user = "root";
$db_pwd = "";
$db_name = "saixinjituan";
//(2)连接MySQL数据库服务器
$link = @mysql_connect($db_host,$db_user,$db_pwd);
if(!$link){
echo "连接数据库失败了!";
exit();//中止程序运行
}
//(3)选择要操作的数据库
if(!mysql_select_db($db_name)){
echo "选择数据库{$db_name}失败!";
exit();
}
//(4)设置MySQL返回的数据字符集
mysql_query("set names utf8");
//(5)执行查询的SQL命令,并返回结果集
$sql = "select * from 007_news order by id asc";
$result = mysql_query($sql); //返回结果集
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
</script>
</head>
<body>
<table width="100%" border="1" align="center" rules="all" cellpadding="5">
<tr>
<th>编号</th>
<th>类型</th>
<th>新闻标题</th>
<th>作者</th>
<th>来源</th>
<th>排序</th>
<th>单击率</th>
<th>发布时间</th>
</tr>
<?php
//通过while循环,每次从结果集中取出一行,
//只能取出所有数据为止,将退出循环。
while($row = mysql_fetch_row($result)){
?>
<tr>
<td><?php echo $row[0];?></td>
<td><?php echo $row[1];?></td>
<td><a href="javascript:void(0)"><?php echo $row[2];?></a></td>
<td><?php echo $row[3];?></td>
<td><?php echo $row[4];?></td>
<td><?php echo $row[7];?></td>
<td><?php echo $row[9];?></td>
<td><?php echo date("Y-m-d H:i",$row[10]);?></td>
</tr>
<?php }?>
</table>
</body>
</html>