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

使用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

猜你喜欢

转载自blog.csdn.net/u014449096/article/details/81285093
099