最早的php对mysql数据库查询是mysql和mysqli方法,后来php的新版本进一步封住了该方法,于是又pdo,抛开php框架,使用pdo查询数据,使用也是相当简便
1 "; 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" }
]