Blogger Information
Blog 17
fans 0
comment 0
visits 12633
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数据库基础与常用操作学习心得
越努力越幸运
Original
1051 people have browsed it

namespace pdo;

//建表语句:

/*

        CREATE TABLE `t_category` (

        `id`                 int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,

        `name`             varchar(50) NOT NULL ,

        `createtime`   date NOT NULL ,

        `status`           int(11) NOT NULL,

        `state`             int(11) NOT NULL

        ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

*/


    return [

             'type'=>$type ?? 'mysql',

             'host'=>$host ?? 'localhost',

             'dbname'=>$dbname ?? 'php7',

             'charset'=>$charset ?? 'utf8',

             'port'=>$port ?? '3306',

             'username'=>$username ?? 'root',

             'password'=>$password ?? ''

    ];


namespace pdo;


use Exception;

use PDO;


        $config=require 'database.php';


//pdo:php data object;

//pdo可以让php对所有类型的数据库,提供了一个统一的,轻量级的访问接口


//连接三要素:

        //数据源:DSN

        //用户名:username

        //密码:password


//DSN:数据库类型:host=数据库的主机地址;dbname=默认的数据库名称;chart=...;port=...;


//$dsn='mysql:host=localhost;dbname=php7;charset=utf8;port=3306';



        $type=$config['type'];

        $host=$config['host'];

        $dbname=$config['dbname'];

        $username=$config['username'];

        $password=$config['password'];


//创建DSN


        $dsn=sprintf('%s:host=%s;dbname=%s',$type,$host,$dbname);


        //echo $dsn;


    try {

             $pdo=new PDO($dsn, $username, $password);

            

             var_dump($pdo);

    

    }catch (Exception $e){

             die($e->getMessage());

    }


namespace pdo;


use PDO;

//查询:

//1.连接数据库-操作数据库-关闭数据库;


        require 'connect.php';

        

        $sql='SELECT * FROM `t_category` WHERE `id`=1';


//预处理对象:为了防止SQL注入

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


        //var_dump($stmt);


//使用预处理对象调用execute()执行这条sql语句

        $stmt->execute();


//调试预处理对象

        //var_dump($stmt->debugDumpParams());


//获取对象:一条:fetch,多条对象fetchall

//结果赋值给$res,结果既有关联数组又有索引数组

        $res=$stmt->fetch();


//只取关联数组:

        $res1=$stmt->fetch(PDO::FETCH_ASSOC);


//注意:以上两个fetch只能运行其中一个,不知道为什么


        var_dump($res);

        echo '<hr>';

        

        var_dump($res1);

        echo '<hr>';


//关闭链接【可选】;


        unset($pdo);


//execute()可以带参数;注释部分是实例:

//匿名占位符:?

//名命占位符::id

/*

        $sql='SELECT * FROM `t_category` WHERE `id`=:id';

        ...

        

        $stmt->execute([':id'=>50]);//冒号可以不用

*/


namespace pdo;


use PDO;

//插入

//1.连接数据库-操作数据库-关闭数据库;


        require 'connect.php';


//2.操作数据库:

        $sql="INSERT `t_category` SET `name`=?,`createtime`=?,`status`=?,`state`=?";

        

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

        

        $data=['c3','20200510',1,1];

        

        $stmt->execute($data);

        

        //判断是否执行成功:$stmt->rowCount()

        if($stmt->rowCount()===1){

                 echo '新增成功,新增记录的主键是:'.$pdo->lastInsertId();

        }else {

                 echo '新增失败';

                 print_r($stmt->errorInfo());

        }


//3.关闭链接【可选】;


        unset($pdo);


namespace pdo;


use PDO;

//修改

//1.连接数据库-操作数据库-关闭数据库;


        require 'connect.php';


//2.操作数据库:

        $sql="UPDATE `t_category` SET `name`=? WHERE `id`=?";

        

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

        

        $data=['c5',5];

        

        $stmt->execute($data);

        

        //判断是否执行成功:$stmt->rowCount()

        if($stmt->rowCount()===1){

                 echo '修改成功';

        }else {

                 echo '没有记录被修改';

                 print_r($stmt->errorInfo());

        }


//3.关闭链接【可选】;


        unset($pdo);


namespace pdo;


use PDO;

//删除

//1.连接数据库-操作数据库-关闭数据库;


        require 'connect.php';


//2.操作数据库:

        $sql="DELETE FROM `t_category` WHERE `id`=:id";

        

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

        //这里需要用过滤器函数

        $id=filter_input(INPUT_GET,'id',FILTER_VALIDATE_INT);

        $stmt->execute(['id'=>$id]);

        

        //判断是否执行成功:$stmt->rowCount()

        if($stmt->rowCount()===1){

                 echo '删除成功';

        }


//3.关闭链接【可选】;


        unset($pdo);









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