Blogger Information
Blog 49
fans 0
comment 4
visits 41881
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
使用PDO进行数据表的新增,更新与删除操作练习
过儿的博客
Original
1754 people have browsed it

使用PDO进行数据表的新增,更新与删除操作练习

1、更新操作

实例

<?php
  $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root');
 $sql ='UPDATE `staff` SET `position` = :position WHERE `id` = :id';
 $stmt = $pdo->prepare($sql);
$stmt->bindValue('position','文化馆长',PDO :: PARAM_STR);
$stmt->bindValue('id',5,PDO::PARAM_INT);

$stmt->execute();
if($stmt->rowCount()>0){
   echo "成功的更新了".$stmt->rowCount().'条记录';
}
$pdo = null;
?>

运行实例 »

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

2、删除操作

实例

<?php
  $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root');
 $sql ='DELETE FROM `staff` WHERE `id` = :id';
 $stmt = $pdo->prepare($sql);
$stmt->execute(['id'=>4]);
if($stmt->rowCount()>0){
   echo "成功的删除了".$stmt->rowCount().'条记录';
}
$pdo = null;
?>

运行实例 »

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

添加操作

实例

<?php
  $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root');
 $sql ='INSERT INTO `staff`(`name`,`age`,`sex`,`position`,`mobile`,`hiredate`) VALUES (:name,:age,:sex,:position,:mobile,:hiredate)';
 $stmt = $pdo->prepare($sql);
$name = '花二娘';
$age = 30;
$sex = 1;
$position = '莲花教主';
$mobile = '13473921234';
$hiredate = time();
 $stmt->bindParam(':name',$name,PDO::PARAM_STR,20);
$stmt->bindParam(':age',$age,PDO::PARAM_INT);
$stmt->bindParam(':sex',$sex,PDO::PARAM_INT);
$stmt->bindParam(':position',$position,PDO::PARAM_STR,20);
$stmt->bindParam(':mobile',$mobile,PDO::PARAM_STR,11);
$stmt->bindParam(':hiredate',$hiredate,PDO::PARAM_INT);
$stmt->execute();
if($stmt->rowCount()>0){
   echo "成功的添加了".$stmt->rowCount().'条记录';
}
$pdo = null;
?>

运行实例 »

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


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
1 comments
郑某某 2019-03-03 09:46:49
好东西
1 floor
Author's latest blog post