【二十一】基于mysqli的表格数据练习

mysqlitest.php

<?php
    //调用数据库的函数
    function connetionsql(){
        $conn=mysqli_connect("127.0.0.1",'root','','user');
        if (!$conn) {
            die("连接失败".mysqli_error());
        }
        mysqli_set_charset($conn,"utf8");
        $sql="select * from user1";
        $res=mysqli_query($conn,$sql);
        // mysqli_affected_rows()返回前一次 MySQL 操作所影响的记录行数。
        // $row=mysqli_affected_rows($conn);
        // mysqli_num_fields()    返回结果集中字段的数量。
         $col=mysqli_num_fields($res);
        echo "<table border='1'><tr>";
        // 取出字段名作为表名
        for ($i=0; $i <$col ; $i++) { 
        // mysqli_fetch_field_direct()    从结果集中取得某个单一字段的 meta-data,并作为对象返回。
            $file_name=mysqli_fetch_field_direct($res,$i);
            // echo $file_name->name;
            echo "<th>$file_name->name</th>";
        }
        echo "</tr>";
        // mysqli_fetch_row()    从结果集中取得一行,并作为枚举数组返回。
        while ($row=mysqli_fetch_row($res)) {
            echo "<tr>";
            for ($i=0; $i <$col ; $i++) { 
                echo "<td>$row[$i]</td>";
            }
            echo "</tr>";
        }
        echo "</table>";
    }
    connetionsql();
?>

实现结果:

解析后的html源码:

<table border='1'>
    <tr>
        <th>id</th><th>name</th><th>password</th><th>email</th><th>age</th>
    </tr>
    <tr>
        <td>4</td><td>huahua</td><td>e10adc3949ba59abbe56e057f20f883e</td><td>huahua@qq.com</td><td>16</td>
    </tr>
    <tr>
        <td>32</td><td>haha</td><td>e10adc3949ba59abbe56e057f20f883e</td><td>test@qq.com</td><td>16</td>
    </tr>
    <tr>
        <td>28</td><td>test</td><td>e10adc3949ba59abbe56e057f20f883e</td><td>test@qq.com</td><td>16</td>
    </tr>
    <tr>
        <td>31</td><td>嘿嘿</td><td>e10adc3949ba59abbe56e057f20f883e</td><td>test@qq.com</td><td>16</td>
    </tr>
</table>

下面是在过程中熟悉的mysqli函数:

mysqli_fetch_fields()

        // mysqli_fetch_fields()    返回结果中代表字段的对象的数组。
        $field_info=mysqli_fetch_fields($res);
        var_dump($field_info);
                //返回的是一个二位数组,如下:
               // array(5) { 
        // [0]=> object(stdClass)#3 (13) 
        // { ["name"]=> string(2) "id" 
        // ["orgname"]=> string(2) "id" 
        // ["table"]=> string(5) "user1" 
        // ["orgtable"]=> string(5) "user1" 
        // ["def"]=> string(0) "" 
        // ["db"]=> string(4) "user" 
        // ["catalog"]=> string(3) "def" 
        // ["max_length"]=> int(2) 
        // ["length"]=> int(11) 
        // ["charsetnr"]=> int(63) 
        // ["flags"]=> int(49667) 
        // ["type"]=> int(3) 
        // ["decimals"]=> int(0) 
        // } 
        foreach ($field_info as $key =>$value) {
            // echo "<br/>".$val->name;
            echo "<br/>.-----";
            echo $key;
            echo "=========";
            echo $value->name;
        }

结果:

//数字为下标
.-----0=========id
.-----1=========name
.-----2=========password
.-----3=========email
.-----4=========age

mysqli_fetch_field() 

        // mysqli_fetch_field() 函数从结果集中取得下一字段(列),并作为对象返回。
        while ($field_info=mysqli_fetch_field($res)) {
            echo "<br/>".$field_info->name;
        }

结果:

id
name
password
email
age
posted @ 2017-12-03 13:07  花花妹子。  阅读(324)  评论(0编辑  收藏  举报