纯php代码打印数据表

下面是一段简单的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>

原文地址:http://liuyanzhao.com/1894.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半缘修道半缘君丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值