PDO MySQL connection

1. What is PDO?
 php data object, php data object, PHP Data Objects  (PDO) extension defines a lightweight uniform interface PHP more database operations. Each PDO database driver implements the interface characteristics of the particular database may be disclosed as a standard extension. Note the use of the PDO extension itself does not implement any database functionality; you must use a  PDO specific database drivers  to access the database service.

PDO provides a  data access  abstraction layer, which means that, regardless of which database you use, you can query and retrieve data using the same functions (methods). PDO  does not provide  a database  abstraction layer; it does not rewrite the SQL, the analog characteristics will not deleted. If necessary, you should use a full-blown abstraction layer.

2. Why use PDO?
Replace other database when the need to replace the code, improve process efficiency

3, PDO is characterized by what?
(1), coding consistency    

(2), flexibility  

(3), object-oriented features   

(4), high-performance

The advantage of using PDO is: fundamentally prevent SQL injection

4, how to use PDO?

Add MYSQL modify php.ini configuration PDO extension

(1) extension = semicolon to the foregoing php_pdo_mysql.dll

(2) have respective extensions directory file extension

(3) PDO to connect different databases, have different database file that is driving us to join the expanded configuration files

(4) Restart Apache validate the configuration
or PHPstudy provided as follows:

 

5, using the basic format PDO

 

 (1) Format

                    $host = "127.0.0.1";  // MySQL所在的服务器的IP

                    $port = "3306";        // MySQL的端口

                    $username= "root";    //数据库账号

                   $password = "123456";  //数据库密码

                   $dbname = "test";   // 数据库名称

                   $charset = "utf8";  // 编码集

                   $dsn = "mysql:dbname=$dbname;host=$host";    // $dsn =“数据库类型:dbname=数据库名;host=数据库的域名”;   

                  备注:  dsn:  data  sourse  name数据的来源  ->要找的数据库是哪台电脑哪个数据库

(2) 声明对象:

                $object = new PDO($dsn,$user,$password);

6、案例:       

 主要思路:
                         1)连接数据库、数据库的用户名、数据库的密码
                         2)生成PDO对象
                         3)执行SQL语句

                     $host = "127.0.0.1"; 
                     $port = "3306"; // 
                     $username = "root"; 
                     $password = "asdf1234"; 
                     $dbname = "tperp"; 
                     $charset = "utf8"; 
                     $dsn = "mysql:dbname=$dbname;host=$host";

       try{
            $pdo = new Pdo($dsn, $username, $password);
             $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    // 设置sql语句查询如果出现问题 就会抛出异常
            set_exception_handler("cus_exception_handler");
         } catch(PDOException $e){
                die("连接失败: ".$e->getMessage());
         }

         function cus_exception_handler($e)
        {
               die("sql 异常: ".$e->getMessage());
         }

         //查询数据
      $state = $pdo->query("select * from admin_user where id = 1");
      //  query执行一条SQL语句,如果通过,则返回一个PDOStatement对象,可以直接遍历这个返回的记录集 (query用于select)

        $res = $state->fetch(PDO::FETCH_ASSOC);      // 获取结果集中的一行数据

        $res = $state->fetchAll(PDO::FETCH_ASSOC);  //获取结果集中的所有数据

 

       //添加数据
        $row1 = $pdo->exec("insert into admin_user(user_name, password) values('jack', 'adsfadf')");
      //  exec主要执行没有返回结果集的操作 PDO::exec执行一条SQL语句,并返回受影响的行数。此函数不会返回结果集合.。 (exec用于insert、delete、update)

 

       //修改数据

         $row2 = $pdo->exec("update admin_user set user_name = 'peter' where user_id = 50");


       //删除数据
        $row3 = $pdo->exec("delete from  admin_user where user_id = 50");

 

        var_dump($res,$row1,$row2,$row3);

 

 

     补充:   

           预处理(防止SQL注入)
           $state = $pdo->prepare("select * from tp_admin_user where user_id = ?");
           $state->execute([49]);

           $row = $state->fetch(PDO::FETCH_ASSOC); // 获取结果集中的一条
           $rows = $state->fetchAll(PDO::FETCH_ASSOC); // 获取结果集中的所有

           echo "<pre>";
           var_dump($rows);

 

Guess you like

Origin www.cnblogs.com/lc2817/p/11295120.html