<?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); //取得數量 echo "Row count:" .$data->rowCount() . "<br>"; //取得陣列數量 echo "Array count:" . count($rows) . "<br>"; foreach ($rows as $key => $value) { var_dump($value); echo "<br>"; } }catch(PDOException $ex){ echo $ex; } ?>
參考資料:
http://www.php.net/manual/en/pdostatement.rowcount.php
http://tw2.php.net/count