Blogger Information
Blog 17
fans 0
comment 0
visits 11922
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
mysqli面向对象的查询操作
Aken的博客
Original
913 people have browsed it

MYSQLI查询实例

<?php
/**
 * mysqli实现数据查询
 */

//连接数据库
$conn = mysqli_connect('127.0.0.1','root','root','php');

//判断是否连接成功、连接错误返回错误信息
if($conn->connect_errno){
    die('连接失败:'.$conn->connect_errno.':'.$conn->connect_error);
}

//设置字符编码
$conn->set_charset('UTF8');

//准备sql语句
$sql = 'SELECT `id`,`name`,`email` FROM `user` WHERE `id` > ?';

//创建预处理对象
$stmt = $conn->stmt_init();

if($stmt->prepare($sql)){

    //绑定参数
    $stmt->bind_param('i',$id);
    //设置参数
    $id = 1;

    //执行查询
    if($stmt->execute()){
        //将获取的结果放到缓存区
        $stmt->store_result();
        //将结果集绑定到变量上
        $stmt->bind_result($id,$name,$email);
        //判断查询的结果集是否为空,不为空的话将结果遍历出来
        if($stmt->num_rows>0){
            //循环遍历结果集
            while($stmt->fetch()){
                echo 'id:'.$id.'姓名:'.$name.'邮件:'.$email ,'<br>';
            }
        }else{
            exit('当前没有你要查询的结果集');
        }

        //释放结果集
        $stmt->free_result();
    }else{
        //返回sql语句检测阶段的出错信息
        exit($stmt->errno.':'.$stmt->error);
    }

}else{
    //返回执行阶段的出错信息
    eixt($stmt->errno.':'.$stmt->error);
}

//注销预处理stmt对象
$stmt->close();
//关闭数据库连接
$conn->close();

运行实例 »

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


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