Details of php pdo object usage: connecting database and exec method

To use pdo, you need to open the pdo extension first. I have opened the pdo extension of mysql here

ghostwu@dev:~$ php -m | grep pdo
pdo_mysql
ghostwu@dev:~$ 

1. Connect to the database

mysql> show create database shop \G;
*************************** 1. row ***************************
       Database: shop
Create Database: CREATE DATABASE `shop` /*!40100 DEFAULT CHARACTER SET utf8 */
1 row in set (0.00 sec)
mysql> show create table account \G;
*************************** 1. row ***************************
       Table: account
Create Table: CREATE TABLE `account` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(20) CHARACTER SET latin1 NOT NULL,
  `user_pwd` varchar(40) CHARACTER SET latin1 NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
try{
        $dsn = 'mysql:host=localhost;dbname=shop';
        $username = 'root';
        $pwd = 'root';
        $pdo = new PDO( $dsn, $username, $pwd );
        var_dump( $pdo );
    }catch( PDOException $e ) {
        echo $e->getMessage();
    }

>Above is the parameter form to connect to the database

>Connecting database in URI form

dsn.txt

mysql:host=localhost;dbname=shop;
try{
        $dsn = 'uri:file:///home/ghostwu/php/php2/pdo/dsn.txt';
        $username = 'root';
        $pwd = 'root';
        $pdo = new PDO( $dsn, $username, $pwd );
        var_dump( $pdo );
    }catch( PDOException $e ) {
        echo $e->getMessage();
    }

Another way is to write dsn connection information in php.ini, which is not recommended

2. exec executes an sql statement, and the return value is the number of affected rows. If there are no affected rows, the return value is 0, which is not valid for select statement

try{
        $dsn = 'mysql:host=localhost;dbname=shop';
        $username = 'root';
        $pwd = 'root';
        $pdo = new PDO( $dsn, $username, $pwd );

        $sql =<<< SQL
        create table if not exists user(
        id int unsigned not null auto_increment,
        username varchar( 20 ) not null unique,
        pwd char( 32 ) not null,
        email varchar( 30 ) not null,
        primary key( id )    
        )engine myisam;
SQL;
        $res = $pdo->exec( $sql );
        var_dump( $res );
    }catch( PDOException $e ) {
        echo $e->getMessage();
    }

3. Execute the insert statement

$insertUserSql = "insert into user( username, pwd, email ) values( 'ghostwu'," . "'" . md5( 'ghostwu' )  . "'" . ",'ghostwu@test.com')";
        $res = $pdo->exec( $insertUserSql );
        var_dump( $res );

4. Execute multiple sql statements at once

 1 try{
 2         $dsn = 'mysql:host=localhost;dbname=shop';
 3         $username = 'root';
 4         $pwd = 'root';
 5         $pdo = new PDO( $dsn, $username, $pwd );
 6         $bajie = md5( 'bajie' );
 7         $wukong = md5( 'wukong' );
 8         $tangsheng = md5( 'tangsheng' );
 9         $insertUserSql =<<<EOF
10         insert into user( username, pwd, email ) values( 'wukong', '$wukong', 'wukong@huaguoshan.com' ),( 'bajie', '$bajie','bajie@tianting.com' ),( 'tangsheng', '$tangsheng','tangsheng@datang.com' );
11 EOF;
12         $res = $pdo->exec( $insertUserSql );
13         var_dump( $res );
14     }catch( PDOException $e ) {
15         echo $e->getMessage();
16     }

5. Obtain the increment id of the last inserted data

    try{
        $dsn = 'mysql:host=localhost;dbname=shop';
        $username = 'root';
        $pwd = 'root';
        $pdo = new PDO( $dsn, $username, $pwd );
        $insertUserSql = "insert into user( username, pwd, email ) values( 'zhanzhao'," . "'" . md5('zhanzhao' ) . "','zhan@kaifeng.com')";
        echo $insertUserSql . PHP_EOL;
        $res = $pdo->exec( $insertUserSql );
        echo $pdo->lastInsertId() . PHP_EOL;
    }catch( PDOException $e ) {
        echo $e->getMessage();
    }

6. Execute delete statement

try{
        $pdo = new PDO( "mysql:host=localhost;dbname=shop", 'root', 'root' );
        $sql = "delete from user where id = 1";
        $res = $pdo->exec( $sql );
        var_dump( $res );
    }catch( PDOException $e ) {
        echo $e->getMessage();
    }

Keywords: PHP PDO MySQL SQL Database

Added by jvanv8 on Fri, 27 Mar 2020 17:03:39 +0200