php中PDO查询结果集3种方法

exec("SET NAMES 'utf8'");

//准备语句
$sql="select * from test2";
$stmt=$conn->prepare($sql);
$stmt->execute();

//方法1
foreach($stmt as $row){
    echo $row[0];
    echo $row["id"];
}

//方法2
while($row=$stmt->fetch(PDO::FETCH_BOTH)){
    echo $row[0];
    echo $row["id"];
}

//方法3
$rows=$stmt->fetchAll(PDO::FETCH_BOTH);
foreach($rows as $row){
    echo $row[0];
    echo $row["id"];
}


?>

你可能感兴趣的:(php中PDO查询结果集3种方法)