本文主要和大家分享Php读取数据的基本操作实例,我们会先和大家分析得哪几步。然后和大家分享代码,希望能帮助到大家。
php的数据库操作的基本步骤:
第1步:链接mysql数据库服务器 $link = mysql_connect(主机,用户名,密码);
第2步:选择需要操作的数据库 mysql_select_db(数据库名字)
第3步:设置字符集
第4步:执行sql语句
第5步:取出数据
以下是操作的步骤实例:
<?php
//数据库的配置信息
$db_host = "localhost";
$db_user = "root";
$db_pwd = "root";
$db_name = "数据库名字";
//前缀
$db_prefix = "007_";
//链接数据库
$link = @mysql_connect($db_host,$db_user,$db_pwd);
if(!$link){
echo "连接mySq数据库失败";
}
//选择当前的数据库
if(!mysql_select_db($db_name,$link)){
echo "打开数据库失败";
}
//设置字符集
mysql_query("set names utf8");
//查询语句
$sql = "select id,title,author,source,hits,addate from {$db_prefix}news order by id desc";
//拿到资源集合
$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 xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
<style type="text/css">
td,th{
padding: 5px;color:#444;font-size: 14px;border: 1px solid #ccc;
}
</style>
</head>
<body>
<table width="1000" border = "1" style="border-collapse: collapse;">
<tr>
<th>编号</th>
<th>标题</th>
<th>作者</th>
<th>来源</th>
<th>点击量</th>
<th>发布时间</th>
</tr>
<?php
while($row = mysql_fetch_row($result)){ //循环取数据
?>
<tr>
<th><?php echo $row[0]?></th>
<th><?php echo $row[1]?></th>
<th><?php echo $row[2]?></th>
<th><?php echo $row[3]?></th>
<th><?php echo $row[4]?></th>
<th><?php echo $row[5]?></th>
</tr>
<?php }?>
</table>
</body>
</html>相关推荐:
以上就是Php读取数据的基本操作的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号