1 頁 (共 1 頁)

[php]mysql_fetch_assoc在php 7中的方式

發表於 : 2020年 5月 6日, 15:53
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