关闭

PHP连接mysql

146人阅读 评论(0) 收藏 举报
分类:
<?php
// by MoreWindows( http://blog.csdn.net/MoreWindows )
//定义常量
define(DB_HOST, 'localhost');
define(DB_USER, 'root');
define(DB_PASS, 'root');
define(DB_DATABASENAME, 'test');
define(DB_TABLENAME, 't_student');
//数据库表的列名
$dbcolarray = array('id', 'name', 'age');

//mysql_connect
$conn = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("connect failed" . mysql_error());
mysql_select_db(DB_DATABASENAME, $conn);

//读取表中纪录条数
$sql = sprintf("select count(*) from %s", DB_TABLENAME);
$result = mysql_query($sql, $conn);
if ($result)
{
        $count = mysql_fetch_row($result);
}
else
{
        die("query failed");
}
echo "表中有$count[0] 条记录<br />";


$sql = sprintf("select %s from %s", implode(",",$dbcolarray), DB_TABLENAME);
$result = mysql_query($sql, $conn);
//表格
echo '<table id="Table" border=1 cellpadding=10 cellspacing=2 bordercolor=#ffaaoo>';
//表头
$thstr = "<th>" . implode("</th><th>", $dbcolarray) . "</th>";
echo $thstr;
//表中的内容
while ($row=mysql_fetch_array($result, MYSQL_ASSOC))//与$row=mysql_fetch_assoc($result)等价
{
        echo "<tr>";
        $tdstr = "";
        foreach ($dbcolarray as $td)
                $tdstr .= "<td>$row[$td]</td>";
        echo $tdstr;
        echo "</tr>";
}
echo "</table>";
mysql_free_result($result);
mysql_close($conn);
?>


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:32911次
    • 积分:1085
    • 等级:
    • 排名:千里之外
    • 原创:74篇
    • 转载:27篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论