[php]mysql_fetch_assoc在php 7中的方式

有關網頁及相關語法的討論
回覆文章
頭像
tim
文章: 1379
註冊時間: 2008年 11月 26日, 00:49

[php]mysql_fetch_assoc在php 7中的方式

文章 tim »

新的 mysqli 與 stmt 語法下, 無法使用原本的 mysql_fetch_assoc 的方式取出資料庫資料, 若要達成類似功能可以使用以下方式:

代碼: 選擇全部

$mysqli = new mysqli('[host]', '[username]', '[passowrd]', '[database]');
if ($stmt = $mysqli->prepare("[sql statement]")) {
    $stmt->execute();
    $meta = $stmt->result_metadata();
    while ($field = $meta->fetch_field())
    {
        $params[] = &$row[$field->name];
    }

    call_user_func_array(array($stmt, 'bind_result'), $params);

    while ($stmt->fetch()) {
        foreach($row as $key => $val)
        {
            $c[$key] = $val;
        }
        $result[] = $c;
    }
   
    $stmt->close();
} 

$mysqli->close();

var_dump($result);
參考資料:

https://www.php.net/manual/en/mysqli-st ... .php#92505
https://www.php.net/manual/en/mysqli-stmt.fetch.php
多多留言, 整理文章, 把經驗累積下來.....
回覆文章