Blogger Information
Blog 15
fans 0
comment 0
visits 10355
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PDO正确获取SELECT查询记录2019.2.25
ken的博客
Original
904 people have browsed it

实例

<?php
//如何正确获取SELECT查询的记录数量
//1.连接数据库

$pdo = new PDO('mysql:host=127.0.0.1;dbname=php;','root','root');

//2.执行操作
$sql = 'SELECT COUNT(*) FROM `staff` WHERE `age`>:age ';
$stmt= $pdo->prepare($sql);
$stmt->bindValue('age',40,PDO::PARAM_INT);
$stmt->execute();


if($stmt->fetchColumn(0)){

    $sql = 'SELECT * FROM `staff` WHERE `age`>:age ';
    $stmt= $pdo->prepare($sql);
    $stmt->bindValue('age',40,PDO::PARAM_INT);
    $stmt->execute();
    foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $value){
        echo '<pre>'; print_r($value);
    }
}


//3.关闭数据库连接

$pdo = null ;

运行实例 »

点击 "运行实例" 按钮查看在线实例

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post