Blogger Information
Blog 33
fans 0
comment 0
visits 24355
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
mysqli与PDO增删改查 2018年8月30日 23:00
EmonXu的博客
Original
631 people have browsed it

连接MYSQL数据库常用的方式有两种,mysqli和PDO.

首先来看mysqli的代码:

实例

<?php

require 'dblink.php';

$sql="insert ignore staff(name,salary) value (?,?)";


$stmt = $mysqli->prepare($sql);

if( !$stmt ) {
    die( $mysqli->error);
}


$name = '勒布朗';
$salary = 20000;

$stmt->bind_param('si',$name,$salary);


if ($stmt->execute()){
    if($stmt->affected_rows>0){
        echo '影响了',$stmt->affected_rows,'条数据,主键为',$stmt->insert_id,'<br>';
    }else{
        echo '没有插入数据','<br>';
    }
}else{
    echo $stmt->errno,':',$stmt->error,'<br>';
}

$sql1="select salary from staff where name=?";

$stmt1=$mysqli->prepare($sql1);

$stmt1->bind_param('s',$name);

if( !$stmt1 ) {
    die( $mysqli->error);
}

if($stmt1->prepare($sql1)){
    $stmt1->execute();
    $stmt1->store_result();
    $stmt1->bind_result($salary);

    if($stmt1->num_rows()>0){
    while($stmt1->fetch()){
        echo $name,'的工资为',$salary;$salary;
    };
}else {
        echo '查无结果';
    }
    // 释放结果集
    $stmt->free_result();
}else{
    echo $stmt1->error;
}

$stmt->close();
$stmt1->close();
$mysqli->close();

运行实例 »

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


再来看PDO的代码:


实例

<?php

$dsn='mysql:host=127.0.0.1;dbname=php';
$user='root';
$pass='root';

try{
    $pdo= new pdo($dsn,$user,$pass);
}catch(PDOException $e){
    die('链接失败!'.$e->getMessage());
}

//关闭
//$pdo = null;
//unset($pdo);

$sql="insert user set name =:name ,email=:email,password=:password";
$sql1="update user set email='emon@163.com' where name='emon'";
$sql2="delete from user where name='emon'";
$sql3="select * from user";

$stmt=$pdo->prepare($sql);
$stmt1=$pdo->prepare($sql1);
$stmt2=$pdo->prepare($sql2);
$stmt3=$pdo->prepare($sql3);

$data=['name'=>'emon','email'=>'emon@qq.com','password'=>'123456'];

$stmt->bindParam(':name',$data['name'],PDO::PARAM_STR);
$stmt->bindParam(':email',$data['email'],PDO::PARAM_STR);
$stmt->bindParam(':password',$data['password'],PDO::PARAM_STR);

if($stmt->execute()){
    echo '成功插入',$stmt->rowCount(),'条记录','<hr>';
}else{
    print_r($stmt->errorInfo());
}

if($stmt1->execute()){
    echo '成功更新',$stmt->rowCount(),'条记录','<hr>';
}else{
    print_r($stmt->errorInfo());
}

if($stmt2->execute()){
    echo '成功删除',$stmt->rowCount(),'条记录','<hr>';
}else{
    print_r($stmt->errorInfo());
}

$stmt3->execute();
//    echo var_export($stmt3->fetchAll());
while($row= $stmt3->fetch($pdo::FETCH_ASSOC)) {
    ECHO var_export($row),'<BR>';
}
ECHO '<hr>';

$stmt4=$pdo->prepare("select count(*) from user");
$stmt4->execute();
echo '本次查询计数:',$stmt4->fetchColumn();

运行实例 »

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


PDO的优势有哪些呢?

PDO有非常多的操作却是MySQL扩展库所不具备的:

1:PDO真正的以底层实现的统一接口数库操作接口,不管后端使用的是何种数据库,如果代码封装好了以后,应用层调用基本上差不多的,当后端数据库更换了以后,应用层代码基本不用修改.

2:PDO支持更高级的DB特性操作,如:存储过程的调度等,mysql原生库是不支持的.

3:PDO是PHP官方的PECL库,兼容性稳定性必然要高于MySQL Extension,可以直接使用 pecl upgrade pdo 命令升级.

4:PDO可以防止SQL注入,确保数据库更加安全


获取结果集记录数量的正确方式是什么?

应该用count(*)和fetchColumn()的方式来获取结果行数。rowCount()适用于insert、update、delete,对于select不适用。

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