纵有疾风起
人生不言弃

PDO中处理预查询语句的方法

<?php

    try{
        $pdo=new PDO(“mysql:host=localhost;dbname=bookstore”,”root”,”158369″);
        echo “数据库连接成功!<br>”;
    }
    catch(PDOexception $e){
        echo “数据库连接失败!”.$e->getmessage().”<br>”;
    }
    
    try{
        //PDO的预处理查询语句
        $statme=$pdo->prepare(“select * from salary where id>5 and id<10”);
        var_dump($statme);
        $statme->execute();
        //将查询到的结果集处理模式设置为索引模式
        $statme->setFetchMode(PDO :: FETCH_NUM );
        //使用预处理类中的PDOStatement::fetch()方法来处理结果集,使用表格输出
        echo “<table align=’center’ width=’800′ border=’1′>”;
            echo “<caption>数据查询结果表</caption>”;
            echo “<tr>”;
            echo “<th>ID</th><th>姓名</th><th>年龄</th><th>学历</th><th>薪水</th>”;
            echo “</tr>”;
        //再次注意:list函数只能接收索引数组的值
        while(list($id,$name,$age,$edu,$salary)=$statme->fetch()){
            echo “<tr align=’center’>”;
                echo “<td>{$id}</td>”;
                echo “<td>{$name}</td>”;
                echo “<td>{$age}</td>”;
                echo “<td>{$edu}</td>”;
                echo “<td>{$salary}</td>”;                
            echo “</tr>”;
        }
        echo “</table>”;    
    }
    catch(PDOexception $e){
        echo “操作查询失败!”.$e->getmessage();
    }
?>

原文链接:https://blog.csdn.net/living_ren/article/details/77649950

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

未经允许不得转载:起风网 » PDO中处理预查询语句的方法
分享到: 生成海报

评论 抢沙发

评论前必须登录!

立即登录