第099讲 php数据库编程⑾-使用mysqli扩展库增强(预处理技术)

本文介绍了如何使用PHP的stmt增强扩展进行预编译查询,并展示了具体的实现代码及效果。此外,还提供了使用面向对象风格操作MySQL数据库的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用stmt增强扩展进行查询

<?php
    header("content-type:text/html;charset=utf-8");
    //预编译查询
    $mysqli = new MySQLi('localhost','root','tmdqobn','db100');
    if($mysqli->connect_error){
        die($mysqli->connect_error);
    }
    //创建一个预定义对象附带占位符
    $sql = "select id,enword from words where id>?";
    $mysqli_stmt  = $mysqli->prepare($sql);
    $id=20;
    //绑定参数
    $isbind_paramSuccess=$mysqli_stmt->bind_param("i",$id);
    if(!$isbind_paramSuccess){
        echo "绑定参数出错";
        return;
    }
    //绑定结果集
    $isbind_result=$mysqli_stmt->bind_result($id,$enword);
    if(!$isbind_result){
        echo "绑定结果集出错";
        return;
    }
    //执行
    $mysqli_stmt->execute();
    //取出值
    while($mysqli_stmt->fetch()){
        echo "--".$id."----".$enword."------<br/>";
    }




    $id=25;
    //绑定参数
    $isbind_paramSuccess=$mysqli_stmt->bind_param("i",$id);
    if(!$isbind_paramSuccess){
        echo "绑定参数出错";
        return;
    }
    //绑定结果集
    //$isbind_result=$mysqli_stmt->bind_result($id,$enword);
    if(!$isbind_result){
        echo "绑定结果集出错";
        return;
    }
    //执行
    $mysqli_stmt->execute();
    //取出值
    echo "-------------------------------------------------------------------------------<br/>";
    while($mysqli_stmt->fetch()){
        echo "--".$id."----".$enword."------<br/>";
    }
    $mysqli_stmt->free_result();
    $mysqli->close();
?>

效果:

--21----0------
--22----1------
--23----2------
--24----3------
--25----4------
--26----5------
--27----6------
--28----7------
--29----8------
--30----9------
-------------------------------------------------------------------------------
--26----5------
--27----6------
--28----7------
--29----8------
--30----9------

image
用上边的方法 执行sql注入之后 整个数据就爆出来了。
image
image

mysqli 增强里边的 其他一些用法请具体参照php手册

<?php
    //mysqli操作mysql数据库(面向对象风格)
    //1、创建mysqli对象
    $mysqli = new MySQLi('localhost','root','tmdqobn','db100');
    echo "这是啥 ".$mysqli->client_info." 错误链接信息 ".$mysqli->connect_errno;
    if($mysqli->connect_errno){//0表示没有错误返回  这个很尴尬 都是 0 表示 false 1 表示true  这个为true的时候表示出错。表示是不是脑子有坑
        //上边这个判断可以用$mysqli->connect_errno 也可以用$mysqli->connect_error; 返回数据为null表示链接成功,如果返回有error则表示sorry出错了
        die('Connect Error:'.$mysqli->connect_errno); //die 等同于exit();
    }
    //2、操作数据库(发送sql)
    $sql = "select * from words";
    $res = $mysqli->query($sql);
    echo "<br/>";
    //3、处理结果
    if(($res->num_rows)>0){//$res 是结果集  这个结果集不能直接用来if判断
        echo "res ".$res->num_rows;
        echo "<table border='1px' width='400px'>";
        while($fetch_Obj = $res->fetch_field()){
            echo "<th>{$fetch_Obj->name}</th>";
        }

        while($row = $res->fetch_assoc()){  // fetch_assoc 是关联数组  fetch_row是索引数组
            //echo "s ".$row['enword']." ".$row['chword'];
            echo "<tr><td>{$row['id']}</td><td>{$row['enword']}</td><td>{$row['chword']}</td></tr>";
        }
        echo "</table>";
        $res->free();
    }

?>

效果(上边根据fetch_filed返回对象取出表列名):

id  enword  chword
1   one2   two3   thr 三
4   four5   five6   six10  boys    男孩儿们
9   boy 男
11  wc  厕所
12  girl    女
13  girls   女孩们
14  test    测试
15  test    测试
16  phone   手机
17  telphone    手机
18  test1   测试1
19  phone1  手机1
20  telphone1   手机1
21  0   100
22  1   101
23  2   102
24  3   103
25  4   104
26  5   105
27  6   106
28  7   107
29  8   108
30  9   109
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有时有晌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值