返回完成运用PDO......登陆

完成运用PDO对数据表进行写操作练习

҈果҈果҈果҈ ҈ ҈2019-04-14 11:59:17280

20190414111541.jpg

  1. 增加(注意本语句需要命名占位符需要在INSERT 后面加入IGNORE

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

<?php

$pdo new PDO('mysql:host=127.0.0.1;dbname=php_io','root','root');

$sql = "INSERT IGNORE INTO `user`(`name`,`sex`,`age`,`email`,`password`,`status`,`create_time`)

          VALUES (:user_name,:sex,:age,:email,:password,:status,:create_time)";

$stmt $pdo->prepare($sql);

$name '宋江';

$sex '0';

$age '58';

$email 'zong@php.cn';

$password = sha1('aaad');

$status '1';

$create_time = time();

 

$stmt->bindParam(':user_name',$name,$pdo::PARAM_STR,100);

$stmt->bindParam(':sex',$sex,$pdo::PARAM_INT,2);

$stmt->bindParam(':age',$age,$pdo::PARAM_INT);

$stmt->bindParam(':email',$email,$pdo::PARAM_STR,200);

$stmt->bindParam(':password',$password,$pdo::PARAM_STR,40);

$stmt->bindParam(':status',$status,$pdo::PARAM_INT);

$stmt->bindParam(':create_time',$create_time,$pdo::PARAM_INT);

 

if($stmt->execute()){

    echo ($stmt->rowCount()>0) ? '成功添加了 '.$stmt->rowCount().' 条记录' '没有记录被添加';

}else{

    exit(print_r($stmt->errorInfo(),true));

}

20190414114314.jpg

2.更新操作

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<?php

$pdo new PDO('mysql:host=127.0.0.1;dbname=php_io','root','root');

$sql "UPDATE `user` SET `name`=:user_name,`email` = :email WHERE `id` = :id";

$stmt $pdo->prepare($sql);

$name ='及时雨';

$email 'adw@php.cn';

$id = 18;

 

$stmt->bindParam(':user_name',$name,$pdo::PARAM_STR,100);

$stmt->bindParam(':email',$email,$pdo::PARAM_STR,200);

$stmt ->bindValue(':id',$id,$pdo::PARAM_INT);

 

 

if($stmt->execute()){

    echo ($stmt->rowCount()>0) ? '成功更新了 '.$stmt->rowCount().' 条记录' '没有记录被更新';

}else{

    echo '更新数据库失败';

    exit(print_r($stmt->errorInfo(),true));

}

20190414115358.jpg

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<?php

$pdo new PDO('mysql:host=127.0.0.1;dbname=php_io','root','root');

$sql "DELETE FROM `user` WHERE `id`=:id";

$stmt $pdo->prepare($sql);

 

$id = 5;

 

$stmt->bindParam(':id',$id,$pdo::PARAM_INT);

 

if($stmt->execute()){

    echo ($stmt->rowCount()>0) ? '成功删除了 '.$stmt->rowCount().' 条记录' '没有记录被删除';

}else{

    echo '删除数据库失败';

    exit(print_r($stmt->errorInfo(),true));

}

20190414115815.jpg

最新手记推荐

• 用composer安装thinkphp框架的步骤• 省市区接口说明• 用thinkphp,后台新增栏目• 管理员添加编辑删除• 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消回复发送