Blogger Information
Blog 16
fans 0
comment 1
visits 18708
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
mysql查询,pdo数据库连接,pdo增删改查-2018-08-30
安丰的博客
Original
1375 people have browsed it

实例

<?php

require 'demo1.php';

$sql ="SELECT `id`,`name`,`salary` FROM `staff` WHERE `salary`>?;";

$stmt=$mysqli->stmt_init();

if ($stmt->prepare($sql)){
    $stmt->bind_param('i',$salary);
    $salary=50;
    if($stmt->execute()){
        //讲查询结果集放入缓存区
        $stmt->store_result();
        //讲结果集 绑定到变量上
         $stmt->bind_result($id,$name,$salary);

         if($stmt->num_rows>0){
             //循环遍历结果集
             while ($stmt->fetch()){
                 echo '<p>ID:'.$id.'---姓名:'.$name.'----月薪:'.$salary.'</p>';
             }
         }else{
             echo '未查询数据';
         }

    }else{

        exit('执行阶段的错误信息'.$stmt->errno.':'.$stmt->error);
    }


}else{

    exit('语句检测发现错误'.$stmt->errno.':'.$stmt->error);
}

运行实例 »

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

实例

<meta charset="utf-8">

<?php
$dsn='mysql:host=127.0.0.1;dbname=php'; //数据源 设置数据库类型,数据库服务器,默认数据库

$user='root'; //数据库用户名
$pass='root';// 数据库密码

//实例化PDO类,创建POD对象,并完成数据库的连接
//$pdo= new PDO($dsn,$user,$pass);

//数据库链接验证
try{
    $pdo= new PDO($dsn,$user,$pass);
    echo '连接成功';
} catch ( PDOException $e){
    die('Connect ERROR!:'.$e->getMessage());
}

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

运行实例 »

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

实例

<?php
//连接数据库
$pdo=new PDO('mysql:host=127.0.0.1;dbname=php','root','root');
//准备SQL 语句, 占位符 使用

$sql="INSERT `user` SET `name`=:name,`email`=:email,`password`=sha1(:password)";
$stmt=$pdo->prepare($sql);
//echo $stmt->queryString;exit; 验证预处理语句的

//绑定参数
$data=['name'=>'杨2过','email'=>'yg@php.cn','password'=>'123'];
//参数绑定,1.占位符 2.数组中对于变量名 3.明确字符类型
$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()){
    //rowCount :返回受影响的记录数量
    if($stmt->rowCount()>0){
        echo '共添加了'.$stmt->rowCount().'条记录';
    } else{
        echo'未添加数据';
    }


}else{
    echo'添加失败';
    print_r($stmt->errorInfo());
    exit();
}

$stmt=null;

运行实例 »

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

实例

<?php
$pdo=new PDO('mysql:host=127.0.0.1;dbname=php','root','root');
$sql="UPDATE `user` SET `email`=:email WHERE `id`=:id";

$stmt=$pdo->prepare($sql);
$data=['email'=>'yzf@qq22.com','id'=>2];
if($stmt->execute($data)>0){
    echo '成功更新了'.$stmt->rowCount().'条信息';
}else{
    echo'无更新';
    print_r($stmt->errorInfo());
    exit();
}

运行实例 »

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

实例

<?php
$pdo=new PDO('mysql:host=127.0.0.1;dbname=php','root','root');
$sql="DELETE FROM `user`  WHERE `id`=:id ";
$stmt=$pdo->prepare($sql);

$date=['id'=>2];
if($stmt->execute($date)>0){
    echo '成功删除了'.$stmt->rowCount().'条信息';
}else{
    echo'无删除';
    print_r($stmt->errorInfo());
    exit();
}

//$pdo=new PDO('mysql:host=127.0.0.1;dbname=php','root','root');
//$sql="DELETE FROM `user` WHERE `id`=:id";
//$stmt=$pdo->prepare($sql);
//if($stmt->execute(['id'=>5])){
//    echo '成功删除了'.$stmt->rowCount().'条信息';
//}else{
//    echo'无删除';
//    print_r($stmt->errorInfo());
//    exit();
//}

运行实例 »

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

实例

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

$sql="SELECT `name`,`salary`FROM `staff`  WHERE `salary`<:salary ";
$stmt=$pdo->prepare($sql);

$stmt->execute(['salary'=>5000]);
//一次性取出
//$rows=$stmt->fetchall();
////var_dump($rows);
//
//foreach($rows as $row){
//    echo '姓名:',$row['name'].'----月薪:',$row['salary'].'<br>';
//}


//循环取出

//$row=$stmt->fetch(PDO::FETCH_ASSOC);
//print_r($row);

while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
    echo var_export($row).'<br>';
}

运行实例 »

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

问答1:PDO的优势:兼容性高可以兼容任何类型的数据了,编写方便

问答2:如何获取结果集记录数据的正确方式

可通过PDO 对结果进行遍历 获取并使用FETCH_BOUND 绑定到一个PHP变量中



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