วันพุธที่ 28 ตุลาคม พ.ศ. 2558

PHP - ดึงข้อมูลจาก database โดยใช้ query ของ mysqli

สำหรับคำสั่ง select ของ mysql นั้น query ของ mysqli จะ return เป็น mysqli_result

mysqli_result::$current_field — Get current field offset of a result pointer
mysqli_result::data_seek — Adjusts the result pointer to an arbitrary row in the result
mysqli_result::fetch_all — Fetches all result rows as an associative array(MYSQLI_ASSOC หรือ 1), a numeric array(MYSQLI_NUM หรือ 2), or both(MYSQLI_BOTH หรือ 3)
mysqli_result::fetch_array — Fetch a result row as an associative, a numeric array, or both
mysqli_result::fetch_assoc — Fetch a result row as an associative array
mysqli_result::fetch_field_direct — Fetch meta-data for a single field
mysqli_result::fetch_field — Returns the next field in the result set
mysqli_result::fetch_fields — Returns an array of objects representing the fields in a result set
mysqli_result::fetch_object — Returns the current row of a result set as an object
mysqli_result::fetch_row — Get a result row as an enumerated array
mysqli_result::$field_count — Get the number of fields in a result
mysqli_result::field_seek — Set result pointer to a specified field offset
mysqli_result::free — Frees the memory associated with a result
mysqli_result::$lengths — Returns the lengths of the columns of the current row in the result set
mysqli_result::$num_rows — Gets the number of rows in a result

fetch_fields

        $fields = $results->fetch_fields();
        foreach ($fields as $f) {
         echo "$f->name :";  
        }
        $results->free();


fetch_assoc

        $fields = $results->fetch_fields();
        echo '<table><tr>';
        foreach ($fields as $f) {
            $name = $f->name;
            echo '<th>' . $name . '</th>';
        }
        echo '</tr>';
        while ($vals = $results->fetch_assoc()) {
            echo "<tr>";
            foreach ($fields as $f) {
                echo '<td>' . $vals[$f->name] . '</td>';
            }
            echo '</tr>';
        }
        echo '</table>';
        $results->free();

fetch_array
       fetch_array ([ int $resulttype = MYSQLI_BOTH ] )
  • MYSQLI_ASSOC ออกมาเป็น associative array
  • MYSQLI_NUM ออกมาเป็น numeric array
  • MYSQLI_BOTH ออกมาทั้ง associative และ numeric แต่ต้องระวัง length จะเพิ่มมาอีกเท่านึงเพราะออกมาทั้ง 2 แบบ ให้ระวังเวลา ใช้ for แบบกำหนด i<length
        $fields = $results->fetch_fields();
        echo '<table><tr>';
        foreach ($fields as $f) {
            $name = $f->name;
            echo '<th>' . $name . '</th>';
        }
        echo '</tr>';
        while ($vals = $results->fetch_array(MYSQLI_ASSOC)) {
            echo "<tr>";
            foreach ($fields as $f) {
                echo '<td>' . $vals[$f->name] . '</td>';
            }
            echo '</tr>';
        }
        echo '</table>';
        $results->free();

หรือ

        $fields = $results->fetch_fields();
        echo '<table><tr>';
        foreach ($fields as $f) {
            $name = $f->name;
            echo '<th>' . $name . '</th>';
        }
        echo '</tr>';
        while ($vals = $results->fetch_array(MYSQLI_NUM)) {
            echo "<tr>";
            for($i=0;$i<count($vals);$i++){
                echo '<td>' . $vals[$i] . '</td>';
            }
            echo '</tr>';
        }
        echo '</table>';
        $results->free();

fetch_all
       fetch_all ([ int $resulttype = MYSQLI_NUM ] )
  • MYSQLI_ASSOC ออกมาเป็น associative array
  • MYSQLI_NUM ออกมาเป็น numeric array
  • MYSQLI_BOTH ออกมาทั้ง associative และ numeric แต่ต้องระวัง length จะเพิ่มมาอีกเท่านึงเพราะออกมาทั้ง 2 แบบ ให้ระวังเวลา ใช้ for แบบกำหนด i<length
        $fields = $results->fetch_fields();
        $rows = $results->fetch_all();
        echo '<table><tr>';
        foreach ($fields as $f) {
            $name = $f->name;
            echo '<th>' . $name . '</th>';
        }
        echo '</tr>';
        for ($i = 0; $i < count($rows); $i++) {
            echo "<tr>";
            for ($j = 0; $j < count($rows[$i]); $j++) {
                echo '<td>' . $rows[$i][$j] . '</td>';
            }
            echo '</tr>';
        }
        echo '</table>';
        $results->free();





ไม่มีความคิดเห็น:

แสดงความคิดเห็น