Blogger Information
Blog 34
fans 0
comment 1
visits 24274
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
【4.25】PHP作业-MySQLi面向对象实现更新操作
51靓号网-专注QQ靓号十年精品
Original
547 people have browsed it

实例

<?php
/**
 * 预处理更新数据
 */

//1.连接数据库,创建pdo对象
try {
$pdo = new PDO('mysql:dbname=php','root','root');
} catch (PDOException $e) {
    exit($e->getMessage());
}

//2.准备sql语句
$sql = "UPDATE user SET email=:email WHERE user_id=:user_id";

//3.创建预处理对象stmt对象
if($stmt = $pdo->prepare($sql)) {

    //4.将参数绑定到stmt对象并执行

    //准备参数
    $param = ['email'=>'lwt@qq.com','user_id'=>5];

    //绑定参数到SQL语句对象并执行
    if ($stmt -> execute($param)){
        //rowCount()返回更新的数量,如果大于0表示有记录被更新啦
        if ($stmt->rowCount()>0) {
            echo '成功更新了'.$stmt->rowCount().'条记录';
        } else {  //等于0表示没有记录被更新
            echo '没有记录被更新';
        }
    } else { //执行失败的信息
        print_r($stmt->errorInfo());
        exit();
    }

} else {  //$stmt语句对象创建失败
  print_r($pdo->errorInfo());
  exit();
}

运行实例 »

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


Correction status:Uncorrected

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