Blogger Information
Blog 38
fans 0
comment 0
visits 29559
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0425,mysqli面向对象更新数据和PDO面向对象更新
riskcn的博客
Original
948 people have browsed it


mysqli_connect.php

实例

<?php
//设置连接参数
$DB_HOST='127.0.0.1';
$DB_USER='root';
$DB_PASS='root';
$DB_NAME='mytest';
$DB_CHAR='utf8';
//创建连接对象
$conn=new mysqli($DB_HOST,$DB_USER,$DB_PASS,$DB_NAME);
//检测连接
if($conn->connect_errno){
exit('连接错误:'.$conn->connect_errno.':'.$conn->connect_error);
}

运行实例 »

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

mysql_pre_update.php

实例

<?php
header("Content-type: text/html; charset=utf-8");
//连接数据库
require 'MYSQLI_CONNECT.php';
//创建sql语句
$sql="UPDATE user SET name=?,age=?,salary=? WHERE ID=7";
//创建stmt对象
$stmt=$conn->stmt_init();
//检测预处理语句
if($stmt->prepare($sql)){
    //绑定预处理数据
    $stmt->bind_param('sii',$name,$age,$salary);
    $name="渣渣辉";
    $age=31;
    $salary=6750;
//    执行stmt语句
    $stmt->execute();
    //判断
    if($stmt->affected_rows>0){
        echo "影响了".$stmt->affected_rows."条数据";
    }else{
        echo "没有更新数据!";
    }
}else{
    exit($stmt->errno.":".$stmt->error);
}
$conn->close();

运行实例 »

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


pdo_update.php

实例

<?php
header("Content-type: text/html; charset=utf-8");
//创建连接对象
$pdo=new PDO('mysql:dbname=mytest','root','root');
//创建预处理sql语句
$sql="UPDATE `user` SET `name`=:name,`age`=:age,`salary`=:salary WHERE `id`=8;";
//创建stmt对象
$stmt=$pdo->prepare($sql);
//绑定数据
$data = ['name'=>'陈小春','age'=>40 ,'salary'=>8140];
$stmt->bindParam(':name',$data['name'],PDO::PARAM_STR);
$stmt->bindParam(':age',$data['age'],PDO::PARAM_INT);
$stmt->bindParam(':salary',$data['salary'],PDO::PARAM_INT);
//提交数据
if($stmt->execute()){

    echo "成功更新了".$stmt->rowCount()."条记录";

}else{

    echo "error:";
    echo $stmt->errorCode();
    print_r($stmt->errorInfo());
    exit();
}
//注销PDO对象
$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
0 comments
Author's latest blog post