夺命雷公狗---PDO NO:14 PDO的预处理查询5(获取结果中的行数)

<?php
header(“Content-Type:text/html;charset=utf-8″);
try{
    $pdo = new PDO(“mysql:host=localhost;dbname=xsphp”,’root’,”);
    $pdo -> setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    $pdo -> query(“set names utf8″);
}catch(PDOException $e){
    echo “数据库连接失败:”.$e -> getMessage();
exit;
}
try{
    $stmt = $pdo -> prepare(“select id,name,age from stu where id > 5 and id <50″);
    //这里是通过?号进行传参
    $stmt -> execute(array(5,50));
    //bindcolumn是用来版定的  由于是通过关联数组方式进行版定所以不用担心顺序问题
    $stmt -> bindColumn(“id”,$id);
    $stmt -> bindColumn(“name”,$name);
    $stmt -> bindColumn(‘age’,$age);
    $stmt -> setFetchMode(PDO::FETCH_NUM);
    echo “<table border=”1>”;
    //fetch()是他自带的一个方法
while($stmt -> fetch()){
    echo “<tr>”;
    echo “<td>id:{$id}</td>”;
    echo “<td>name:{$name}</td>”;
    echo “<td>age:{$age}</td>”;
    echo “</tr>”;
}
echo “</table>”;
    //获取结果中的行数
    echo $stmt -> rowCount();
}catch(PDOException $e){
    echo “错误:”.$e -> getMessage();
}
原文地址:https://www.cnblogs.com/leigood/p/5032840.html