Blogger Information
Blog 17
fans 0
comment 0
visits 11930
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
pdo增删改查操作
Aken的博客
Original
1343 people have browsed it

PDO新增数据实例

<?php
/**
 * PDO新增数据
 */

//连接数据库
$pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root',array(PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES \'UTF8\''));

//创建预处理对象
$stmt = $pdo->prepare("INSERT `user` SET `name`= :name , `email`= :email, `password`= sha1(:password)");

//执行
$stmt -> execute(['name'=>'李林','email'=>'lilin@126.cn','password'=>'123456']);
echo '成功添加了:'.$stmt->rowCount().'条数据';

运行实例 »

点击 "运行实例" 按钮查看在线实例

pdo删除操作实例

<?php
/**
 * pdo删除操作
 */

//连接数据库
$pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root',array(PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES \'UTF8\''));

//创建预处理对象
$stmt = $pdo->prepare('DELETE FROM `user` WHERE `id` = :id');


//执行
if ($stmt->execute(['id'=>4])){
    echo '成功删除:'.$stmt->rowCount().'条数据';
}else{
    exit($stmt->errorInfo());
}

$stmt = null;
$pdo = null;

运行实例 »

点击 "运行实例" 按钮查看在线实例

pdo更新数据实例

<?php
/**
 * pdo更新数据
 */

$pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root',array(PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES \'UTF8\''));

$stmt = $pdo->prepare('UPDATE `user` SET `name`=:name,`email`=:email WHERE `id`=:id');

$stmt->execute(['id'=>8,'name'=>'admin','email'=>'qq@qq.com']);
echo '成功更新'.$stmt->rowCount().'条数据';

运行实例 »

点击 "运行实例" 按钮查看在线实例

pdo查询数据实例

<?php
/**
 * pdo查询数据
 */

$pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root',array(PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES \'UTF8\''));

$stmt = $pdo->prepare('SELECT `id`,`name`,`email` FROM `user` WHERE `id`>:id');

$stmt->execute(['id' => 1]);

//将结果集绑定到变量上
$stmt->bindColumn('id',$id);
$stmt->bindColumn('name',$name);
$stmt->bindColumn('email',$email);

//循环输出结果
while($stmt->fetch(PDO::FETCH_BOUND)){
    echo 'id:'.$id.' 姓名:'.$name.' 邮箱:'.$email ,'<br>';
}

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:qualified

Teacher's comments:
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!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments