[PDO]⑧--prepare()、execute()方法执行查询语句

[PDO]⑧--prepare()、execute()方法执行查询语句_第1张图片
Paste_Image.png
[PDO]⑧--prepare()、execute()方法执行查询语句_第2张图片
Paste_Image.png

$pdo->prepare($sql)

$stmt->execute()

$stmt->fetch()

prepare($sql);
    var_dump($stmt);
    /*
     * object(PDOStatement)[2]
     * public 'queryString' => string 'select * from user where username="king11"' (length=42)
     */
    //execute() 执行预处理语句
    $res = $stmt->execute();
    var_dump($res);//boolean true
    //fetch() 得到结果集中的一条记录
    $row = $stmt->fetch();
    var_dump($row);
    /**
     * array
     * 'id' => string '40' (length=2)
     * 0 => string '40' (length=2)
     * 'username' => string 'king11' (length=6)
     * 1 => string 'king11' (length=6)
     * 'password' => string 'e02cc1e4e6d565fe7df86ce90834f150' (length=32)
     * 2 => string 'e02cc1e4e6d565fe7df86ce90834f150' (length=32)
     * 'email' => string '[email protected]' (length=12)
     * 3 => string '[email protected]' (length=12)
     */
    
} catch (PDOException $e) {
    echo $e->getMessage();
}

?>
[PDO]⑧--prepare()、execute()方法执行查询语句_第3张图片
Paste_Image.png
prepare($sql);
    $res = $stmt->execute();
    if ($res) {
        while ($row = $stmt->fetch()) {
            echo "用户名:" . $row['username'];
            echo "
"; } } } catch (PDOException $e) { echo $e->getMessage(); } ?>
[PDO]⑧--prepare()、execute()方法执行查询语句_第4张图片
Paste_Image.png

$stmt->fetchAll()

$rows = $stmt->fetchAll();
var_dump($rows);

$stmt->fetch(PDO::FETCH_ASSOC)

if ($res) {
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            echo "用户名:" . $row['username'];
            echo "
"; } }

PDO::FETCH_BOTH

 while ($row = $stmt->fetch(PDO::FETCH_BOTH))

PDO::FETCH_OBJ

while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {

PDO::FETCH_ASSOC

$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

$stmt->setFetchMode()

 $stmt->setFetchMode(PDO::FETCH_ASSOC);
    var_dump($stmt->fetchAll());

你可能感兴趣的:([PDO]⑧--prepare()、execute()方法执行查询语句)