Blogger Information
Blog 81
fans 1
comment 0
visits 124688
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PDO操作数据库
有什么是忘不了的的博客
Original
839 people have browsed it
一、PDO连接数据库。
数据库  :chat,  数据表:CONTENT ;
try{
    $pdo = new PDO('mysql:host=127.0.0.1;dbname=chat','root','root');//连接数据库 chat库名
    $pdo->exec("SET names utf8");//编码格式
    }catch(PDOException $e){
    exit($e->getMessage());
    }
    $pdo = null; //关闭数据库。

参数绑定
     //对表中得列绑定
     $stmt->bindColumn(1, $name); //通过列号绑定
     $stmt->bindColumn('user_id ', $user_id ,PDO::PARAM_* ,20);  //通过列名绑定  第三个参数设置数据类型,整数型,字符型等,第四个参数设置数据长度。

     // 对sql 中的命名占位符绑定
     bindParam()//必须使用变量进行传入
     bindValue()//可以直接写到参数上
     $calories = 150;
     $stmt->bindParam(':calories', $calories, PDO::PARAM_INT);
     $stmt->bindValue(':colour', 12, PDO::PARAM_INT);
     
 二、查询数据
    
        $sql = "SELECT id ,content,create_time,user_id FROM `CONTENT` WHERE `id` > :id1 && `id` <:id";//:id是命名占位符。反引号可加可不加。
        //$stmt是PDOStatement类的对象
        $stmt = $pdo->prepare($sql);
        //以关联数组返回,
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        //参数绑定并查询
        if($stmt->execute([':id'=>10,':id1'=>0])){
        //吧结果集中的所有数据赋值给$ret
        $ret = $stmt->fetchAll();
        //从最后一次请求中抓取下一个结果。通过循环可以输出所有数据
        //$ret = $stmt->fetch();
        }else{
        	print_r($stmt->errorInfo());die;
        }

三、添加操作

                            rowCount()方法:返回受影响条数

                            errorlnfo()方法:错误提示

                                $sql ="INSERT INTO CONTENT (id,content,create_time,user_id) VALUES (:id,:content,:create_time,:user_id)";

                                $stmt = $pdo->prepare($sql);

                                //参数绑定

                                $id = null;

                                $content = "我的世界";

                                $create_time = time();

                                $user_id = 50;

                                $stmt -> bindParam(':id',$id);

                                $stmt -> bindParam(':content',$content,PDO::PARAM_STR,100);

                                $stmt -> bindParam(':create_time',$create_time,PDO::PARAM_INT);

                                $stmt -> bindParam(':user_id',$user_id,PDO::PARAM_INT);

                                //执行一条预处理语句

                                if ($stmt->execute()) {

                                 echo $stmt->rowCoUnt();

                                }else{

                                 exit(print_r($stmt->errorInfo(),true));

                                }

四、更新数据

            $sql ="UPDATE  CONTENT SET  content = :content,create_time = :create_time WHERE id = :id";

            其他跟添加一样。

五、删除数据

        一般采用软删除。就是把利用一个删除时间字段,如果字段有时间则不显示。无数据就显示出来。

        $sql ="DELETE  FROM CONTENT WHERE id = :id";

其他与添加一样。

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