$sql = 'select count(*) from tb_root';
$result = $pdo->query($sql);//提交sql
$rowsNumber = $result->fetchColumn();//取回结果集中的一个字段
echo $rowsNumber;
query方法用来提交select语句,exec方法一般用于insert,update, delete等
对于普通的查询,可以这样做
$sql = 'select * from tb_root';
$result = $pdo->query($sql);
$rows = $result->fetchAll(PDO:FETCH_ASSOC);//取得所有行
$rowsNumber = count($rows);
echo $rowsNumber;
$sql = "select * from tb_root";
$res = $pdo->prepare($sql);
//$res->exec();
$res->execute();
$num = $res->rowCount();