Sian 发表于 2016-1-7 10:16:39

PHP中使用mysql模块输出数据库内容


<?php
        // 1、连接数据库
        $mysql = mysql_connect("localhost", "root", "yusian");
        if(!$mysql){
                echo "连接数据库失败<br/>";
                die(mysql_error());
        }
        // 2、设置操作
        // 字符集
        mysql_set_charset($mysql, "uft8");
       
        // 3、选择一个默认数据库使用
        mysql_select_db("think");
       
        // 4、操作数据库的sql语句执行(DML DCL DDL | DQL)
        // 语句分两种:一种返回结果;一种返回真假
        // 返回结果的需要格式化处理
        $sql = "select id, userName, age, score, date from sa_user";
       
        $result = mysql_query($sql);
        // 索引数组mysql_fetch_row()
        // 关联数组mysql_fetch_assoc()
        echo '<table width="60%" border = "1" align = "center">';
        // 制作表头
        echo "<tr>";
        $field_num = mysql_num_fields($result);
        for($i = 0; $i < $field_num; $i++){
                echo "<th>".mysql_field_name($result, $i)."</th>";
        }
        echo "<tr/>";
        // 遍历表格内容
        while($array = mysql_fetch_row($result)){
                // 指针移动使用mysql_data_seek改变位置
                echo '<tr>';
                foreach($array as $a){
                        echo "<td>".$a."</td/>";
                }
                echo '</tr>';
        }
        echo '</table>';
        // 5、关闭数据库连接
        mysql_close($mysql);
页: [1]
查看完整版本: PHP中使用mysql模块输出数据库内容