abstract://添加操作<?php $db=[ 'type'=>'mysql', 'host'=>'127.0.0.1', 'dbname'=>'whyan', 'charset'=>'uft8', 'user'=&g
//添加操作
<?php
$db=[
'type'=>'mysql',
'host'=>'127.0.0.1',
'dbname'=>'whyan',
'charset'=>'uft8',
'user'=>'root',
'password'=>'root'
];
//创建连接
$dsn="{$db['type']}:host={$db['host']};dbname={$db['dbname']}";
//创建对象
try{
$pdo=new PDO($dsn,$db['user'],$db['password']);
//echo '连接成功!';
}catch(PDOExeception $e){
exit($e->getMessage());
}
//创建SQL语句
$sql="INSERT INTO `member`(`qq`,`nickname`,`password`,`avatar`,`add_time`) VALUES(:qq,:nickname,:password,:avatar,:add_time)";
//创建预处理对象
$stmt=$pdo->prepare($sql);
$qq='13886677309';
$nickname='灰太狼';
$password=md5($qq.'123456');
$avatar='/static/image/a9.png';
$add_time=time();
//绑定参数
$stmt->bindParam(':qq',$qq,PDO::PARAM_STR,10);
$stmt->bindParam(':nickname',$nickname,PDO::PARAM_STR,20);
$stmt->bindParam(':password',$password,PDO::PARAM_STR,10);
$stmt->bindParam(':avatar',$avatar,PDO::PARAM_STR);
$stmt->bindParam(':add_time',$add_time,PDO::PARAM_INT);
//执行
if($stmt->execute()){
if($stmt->rowCount()>0){
echo '添加记录成功!';
}
}
$stmt=null;
$pdo=null;
//更新操作
<?php
$db=[
'type'=>'mysql',
'host'=>'127.0.0.1',
'dbname'=>'whyan',
'charset'=>'uft8',
'user'=>'root',
'password'=>'root'
];
//创建连接
$dsn="{$db['type']}:host={$db['host']};dbname={$db['dbname']}";
//创建对象
try{
$pdo=new PDO($dsn,$db['user'],$db['password']);
//echo '连接成功!';
}catch(PDOExeception $e){
exit($e->getMessage());
}
//创建SQL语句
$sql="UPDATE `member` SET `qq`=:qq,`nickname`=:nickname,`add_time`=:add_time WHERE `uid`=:uid";
//创建预处理对象
$stmt=$pdo->prepare($sql);
$uid=9;
$qq='13886677369';
$nickname='大灰狼';
$add_time=time();
//绑定参数
$stmt->bindParam(':uid',$uid,PDO::PARAM_INT);
$stmt->bindParam(':qq',$qq,PDO::PARAM_STR,10);
$stmt->bindParam(':nickname',$nickname,PDO::PARAM_STR,20);
$stmt->bindParam(':add_time',$add_time,PDO::PARAM_INT);
//执行
if($stmt->execute()){
if($stmt->rowCount()>0){
echo '更新记录成功!';
}else{
exit(print_r($stmt->errorInfo(),true));
}
}
//删除操作
<?php
$db=[
'type'=>'mysql',
'host'=>'127.0.0.1',
'dbname'=>'whyan',
'charset'=>'uft8',
'user'=>'root',
'password'=>'root'
];
//创建连接
$dsn="{$db['type']}:host={$db['host']};dbname={$db['dbname']}";
//创建对象
try{
$pdo=new PDO($dsn,$db['user'],$db['password']);
//echo '连接成功!';
}catch(PDOExeception $e){
exit($e->getMessage());
}
//创建SQL语句
$sql="DELETE FROM `member` WHERE `uid`=:uid";
//创建预处理对象
$stmt=$pdo->prepare($sql);
$uid=8;
//绑定参数
$stmt->bindParam(':uid',$uid,PDO::PARAM_INT);
//执行
if($stmt->execute()){
if($stmt->rowCount()>0){
echo '删除记录成功!';
}else{
exit(print_r($stmt->errorInfo(),true));
}
}
//查看操作
<?php
$db=[
'type'=>'mysql',
'host'=>'127.0.0.1',
'dbname'=>'whyan',
'charset'=>'uft8',
'user'=>'root',
'password'=>'root'
];
//创建连接
$dsn="{$db['type']}:host={$db['host']};dbname={$db['dbname']}";
//创建对象
try{
$pdo=new PDO($dsn,$db['user'],$db['password']);
//echo '连接成功!';
}catch(PDOExeception $e){
exit($e->getMessage());
}
//创建SQL语句
//$sql="SELECT count(*) as total `member` WHERE `uid`>:uid";
$sql="SELECT * FROM `member` WHERE `uid`>:uid";
//创建预处理对象
$stmt=$pdo->prepare($sql);
$uid=2;
//绑定参数
$stmt->bindParam(':uid',$uid,PDO::PARAM_INT);
//执行
if($stmt->execute()){
$rows=$stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
//echo '<pre>',var_export($row,true),'<br>';
echo $row['uid'].$row['qq'].$row['nickname'].date('Y/m/d',$row['add_time']),'<br>';
}
}
//创建SQL语句
$sql="SELECT count(*) FROM `member` WHERE `uid`>:uid";
$stmt=$pdo->prepare($sql);
$uid=2;
//绑定参数
$stmt->bindParam(':uid',$uid,PDO::PARAM_INT);
$stmt->execute();
$total=$stmt->fetchColumn(0);
echo $total;
Correcting teacher:天蓬老师Correction time:2019-08-07 13:25:48
Teacher's summary:PDO中的查询是个重点, 多了解一下