<?php
try {
$dsn = 'mysql:dbname=demo';
$userName = 'root';
$password = 'root';
$pdo = new PDO($dsn,$userName,$password);
//创建预处理sql语句
$sql = "SELECT `id`,`name`,`email` FROM `user` WHERE `id`>:id";
//执行预处理sql语句,创建一个PDOStatement
$pdoStmt = $pdo->prepare($sql);
//判断$pdoStmt对象是否创建成功
if ($pdoStmt == true) {
//执行预处理语句:execute()
$res = $pdoStmt->execute(['id'=>5]);
if ($res == true) {
if ($pdoStmt->rowCount() > 0) { //rowCount()返回当前有多少条数据
//第一种: foreach()
/*foreach ($pdoStmt as $row) {
echo '<pre>';
print_r($row);
}*/
//第二种:fetchAll()
/*$pdoStmt->setFetchMode(PDO::FETCH_ASSOC);
$row = $pdoStmt->fetchAll();
echo '<pre>';
print_r($row);*/
//第三种:while()
while ($row = $pdoStmt->fetch(PDO::FETCH_ASSOC)) {
echo '<pre>';
print_r($row);
}
} else {
echo '当前数据表里没有改数据';
}
} else {
echo '当前数据表里没有改数据';
}
} else {
print_r($pdo->errorInfo());//创建失败返回错误语句
}
} catch (PDOException $e) {
print 'Connect ERROR'.$e->getMessage();
dre();
}
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!