Blogger Information
Blog 4
fans 0
comment 0
visits 2308
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
3月22日写一个pdo
大红仓的博客
Original
530 people have browsed it

<?php

    // 连接方法

  function con(){

        $dsn = 'mysql:host=127.0.0.1;dbname=ouyangke;charset=utf8;port=3306';

        $dsn = 'mysql:dbname=yubin';

        $dbname = 'root';

        $dbpw   = 'root';

        try{

            $p = new PDO($dsn,$dbname,$dbpw);

        } catch (PDOException $e) {

            print_r($e->getMessage());

            exit;

        }

        return $p;

    }

    // 1、查询数据

    function select($table,$field='*',$where='',$order='',$limit=''){   

        $c = con(); 

$sql = 'SELECT ';

    

        if(!empty($field)){

            $sql .= rtrim($field);      

        }

        $sql .= ' FROM ';   

        $sql .= $table; 

        if(!empty($where)){

            $sql .= ' WHERE ';

            $sql .= $where;     

        }

        if(!empty($order)){

            $sql .= ' ORDER BY ';

            $sql .= $order;

        }


        if(!empty($limit)){

            $sql .= ' LIMIT ';

            $sql .= $limit;

        }


        $a = $c->prepare($sql);

        // 4,执行sql语句,判断是否执行mysql语句成功

        // 为什么要判断,别人使用咱们写好的方法,可能会传一些,无法估计的参数,导致查询失败。

        if($a->execute()){

            // 当查询结果后,结果集是没有数据的。

            // 用rowCount方法,判断结果集是否有结果,有结果我们才返回结果

            if($a->rowCount()){

                // 5,设置模式

                $a->setFetchMode(PDO::FETCH_ASSOC);

                // 6,结果集

                $ret = $a->fetchAll();

                // 返回结果集

                return $ret;

            }else{

                return false;

            }

        }else{

            return false;

        }

    }

$s = select('xz_user','`uid`,`uname`,`email`','`uid` > 7');

 print_r($s);


运行结果正确,然后其它的增删改没写成功,请老师讲课时指导,谢谢。


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