基本操作
<?php
$pdo=new PDO('mysql:host=localhost;dbname=user2','root','');
$sql="select * from t5";
$smt=$pdo -> query($sql);
$row=$smt->fetchAll(PDO::FETCH_ASSOC);
echo '<pre>';
print_r($row);
echo '</pre>';
?>
预处理(增、删、改)操作
<?php
header('content-type:text/html;charset=utf-8');
$pdo=new PDO('mysql:host=localhost;dbname=user2','root','');
$pdo->exec('set names utf8');
$sql="insert into t5(name,num) values('i','66')";
$smt=$pdo->prepare($sql);
if($smt->execute()){
$tot=$smt->rowCount();
echo "数据插入成功,成功插入{$tot}行";
}else{
echo '数据插入失败';
}
?>
预处理(查)操作
<?php
header('content-type:text/html;charset=utf-8');
$pdo=new PDO('mysql:host=localhost;dbname=user2','root','');
$pdo->exec('set names utf8');
$sql="select * from t5";
$smt=$pdo->prepare($sql);
$smt->execute();
$rows=$smt->fetchAll(PDO::FETCH_ASSOC);
echo "<pre>";
print_r($rows);
echo "</pre>";
?>
网友评论