最早的php對mysql資料庫查詢是mysql和mysqli方法,後來php的新版本進一步封住了該方法,於是又pdo,拋開php框架,使用pdo查詢資料,使用也是相當簡便
1 <?php 2 ini_set('max_execution_time',3600); 3 $pdo = new pdo('mysql:host=localhost;dbname=test','root','123456'); 4 echo "<br/>"; 5 6 $sql = $pdo->prepare('SELECT * FROM persons WHERE Id_P=:id'); 7 $sql->execute(['id'=>1]); 8 $res=$sql->fetchAll(PDO::FETCH_ASSOC); 9 $json=json_encode($res); 10 echo ($json); 11 $pdo=null;
輸出:
[
{
"Id_P":"1",
"LastName":"Adams",
"FirstName":"John",
"Address":"Oxford Street",
"City":"London"
}
]