php读取数据库表

<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP version 5                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available through the world-wide-web at the following url:           |
// | http://www.php.net/license/3_0.txt.                                  |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Original Author <author@example.com>                        |
// |          Your Name <you@example.com>                                 |
// +----------------------------------------------------------------------+
//
// $Id:$

$db_host = "localhost"; //MYSQL服务器名
$db_user = "root"; //MYSQL用户名
$db_pass = "root"; //MYSQL用户对应密码
$db_name = "qiu"; //要操作的数据库
//使用mysql_connect()函数对服务器进行连接,如果出错返回相应信息
$link = mysql_connect($db_host, $db_user, $db_pass) or die("不能连接到服务器" . mysql_error());
mysql_select_db($db_name, $link); //选择相应的数据库,这里选择test库
$sql = "select * from charge"; //先执行SQL语句显示所有记录以与插入后相比较
$result = mysql_query($sql, $link); //使用mysql_query()发送SQL请求
echo "当前表中的记录有:";
echo "<table border=1>"; //使用表格格式化数据
echo "<tr><td>ID</td><td>CDkey</td><td>DueTime</td><td>overdue</td><td>地址</td></tr>";
while ($row = mysql_fetch_array($result)) //遍历SQL语句执行结果把值赋给数组
{
    echo "<tr>";
    echo "<td>" . $row[id] . "</td>"; //显示ID
    echo "<td>" . $row[CDkey] . " </td>"; //CDkey
    echo "<td>" . $row[DueTime] . " </td>"; //DueTime
    echo "<td>" . $row[overdue] . " </td>"; //overdue
    echo "<td>" . $row[url] . " </td>"; //显示地址
    echo "</tr>";
}
echo "</table>";
?>
Comments: 25

「人生在世,留句话给我吧」

提交评论