<?php header("Content-Type:text/html; charset=utf-8"); $db = new PDO('mysql:host=IP;dbname=test', '帳號', '密碼', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8';")); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); try{ $sql = "SELECT * FROM `...`"; $data = $db->query($sql); //一次性讀取 $rows = $data->fetchAll(PDO::FETCH_ASSOC); foreach ($rows as $key => $value) { var_dump($value); } }catch(PDOException $ex){ echo $ex; } ?>
參考資料:
http://stackoverflow.com/questions/14035108/how-to-fetch-row-with-pdo
http://php.net/manual/en/pdostatement.fetch.php
http://blog.longwin.com.tw/2009/07/html-utf8-browser-encoding-solution-2009/