Blogger Information
Blog 42
fans 0
comment 0
visits 15665
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0817常用的CURD操作
小言
Original
252 people have browsed it

1.匿名参数 + 索引数组

  1. $sql = 'INSERT `staff` SET `name`=?, `sex` = ?, `email`=?;';
  2. $data = ['张三', 1, 'zhangsan@qqq.om'];

2.命名参数 + 关联数组

  1. $sql = 'INSERT `staff` SET `name`=:name, `sex` = :sex, `email`=:email;';
  2. $data = ['name' => '小龙女','sex' => 0, 'email' => 'xiaolongnu@qqq.om'];

3.参数绑定:值绑定 bindValue()

  1. $sql = <<< SQL
  2. INSERT `staff`
  3. SET `name` = ?, `sex` = ?, `email` = ?;
  4. SQL;
  5. list($name, $sex, $email) = ['欧阳克', 0 , 'hongqigong@qq.com'];
  6. $stmt->bindValue(1, $name, PDO::PARAM_STR);
  7. $stmt->bindValue(2, $sex, PDO::PARAM_INT);
  8. $stmt->bindValue(3, $email, PDO::PARAM_STR);

4.参数绑定:引用绑定 bindParam()

  1. $stmt->bindParam(1, $name, PDO::PARAM_STR);
  2. $stmt->bindParam(2, $sex, PDO::PARAM_INT);
  3. $stmt->bindParam(3, $email, PDO::PARAM_STR);
  4. list($name, $sex, $email) = ['郭靖456', 0 , 'hongqigong@qq.com'];

5.更新操作

  1. if (false === stripos($sql, 'where')) {
  2. exit('禁止无条件更新');
  3. }
  4. $stmt = $db->prepare($sql);
  5. $data = ['八戒', 0 , 'bajie@qq.com', 6];

6.删除操作

  1. if (false === stripos($sql, 'where')){
  2. exit('禁止无条件删除 ');
  3. }
  4. if($stmt->execute([3])){
  5. if ($stmt->rowCount() > 0){
  6. //成功 success
  7. echo '成功的删除了,' . $stmt->rowCount() . '条记录~~~';
  8. }else{
  9. //主要用于检测是否重复执行
  10. echo '没有记录被删除';
  11. print_r($stmt->errorInfo());
  12. }
  13. }else{
  14. //false
  15. echo 'Sql执行失败';
  16. print_r($stmt->errorInfo());
  17. }

7.查询

  1. $stmt->bindValue(1, 5, PDO::PARAM_INT);
  2. if($stmt->execute()){
  3. while ($staff = $stmt->fetch(PDO::FETCH_ASSOC)){
  4. printf('<pre>%s</pre>', print_r($staff,true));
  5. }
  6. }else{
  7. //false
  8. echo 'Sql执行失败';
  9. print_r($stmt->errorInfo());
  10. }
Correcting teacher:PHPzPHPz

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
Author's latest blog post