获取mysqli函数的值和字段名

<?php
    $mysqli=new mysqli("localhost", "root", "123456", "xsphpdb");

    if(mysqli_connect_errno()){
        echo "错误:".mysqli_connect_error();
        exit;
    }

    //执行select语句,返回来的就是结果集(对象)
    
    $sql="select id cid, name shopname, price shopprice, num shopnum, desn shopdesn from shops";

    $result=$mysqli->query($sql);

    $rows=$result->num_rows;
    $cols=$result->field_count;

    echo "表中{$rows}行,{$cols}列<br>";
    

    //记录信息
    /*  $result->fetch_row()  ----  mysql_fetch_row()      索引数组
     *  $result->fetch_assoc() ---  mysql_fetch_assoc()    关联数组(下标就是列名)
     *  $result->fetch_array()  ---- mysql_fetch_array()   两个数组都返回(MYSQLI_ASSOC, MYSQLI_NUM,MYSQLI_BOTH(default))
     *  $result->fetch_object()  --- mysql_fetch_object()  
     *  
     *  每次执行一次,就会从结果集中取出当前一条记录(当前记录就是第一个行,可以使用data_seek(5))
     *    
     *   指针指向下一行,下次再取时,就会取出下一行,当结果集中没有记录时,则返回false    
     *
     */

    echo '<table border=1 align="center" width=900>';
    echo '<tr>';
//    $result->field_seek(2);
    while($field=$result->fetch_field()){
        echo '<th>'.$result->current_field.'_['.$field->orgname.']'.$field->name.'('.$field->max_length.')</th>';
    }
    echo '</tr>';

//    $result->data_seek(50);
    while($row=$result->fetch_assoc()){
        echo '<tr>';
        foreach($row as $col){
            echo '<td>'.$col.'</td>';
        }
        echo '</tr>';
    }
    echo '</table>';


    $result->free();
    $mysqli->close();

 

posted @ 2015-12-04 16:11  对数螺旋线  阅读(1127)  评论(0编辑  收藏  举报