mysql数据库查询pdo的用法

时间:2023-02-14 23:14:18

最早的php对mysql数据库查询是mysql和mysqli方法,后来php的新版本进一步封住了该方法,于是又pdo,抛开php框架,使用pdo查询数据,使用也是相当简便

 <?php
ini_set('max_execution_time',3600);
$pdo = new pdo('mysql:host=localhost;dbname=test','root','123456');
echo "<br/>"; $sql = $pdo->prepare('SELECT * FROM persons WHERE Id_P=:id');
$sql->execute(['id'=>1]);
$res=$sql->fetchAll(PDO::FETCH_ASSOC);
$json=json_encode($res);
echo ($json);
$pdo=null;

输出:
[
    {
        "Id_P":"1",
        "LastName":"Adams",
        "FirstName":"John",
        "Address":"Oxford Street",
        "City":"London"
    }
]