Detailed explanation of php pdo object usage: connecting to database and exec method

To use pdo, you first need to enable the pdo extension. I have already enabled 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();
    }

> The above is a parameter form to connect to the database

>uri form to connect to the database

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 is to write dsn connection information in php.ini, which is not recommended.

2. exec executes a sql statement, the return value is the number of affected rows, if there is no number of affected rows, the return value is 0, this method is invalid for the 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' )  . "'" . ",'[email protected]')";
        $res = $pdo->exec( $insertUserSql );
        var_dump( $res );

4. Execute multiple sql statements at one time

 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', '[email protected]' ),( 'bajie', '$bajie','[email protected]' ),( 'tangsheng', '$tangsheng','[email protected]' );
11 EOF;
12         $res = $pdo->exec( $insertUserSql );
13         var_dump( $res );
14     }catch( PDOException $e ) {
15         echo $e->getMessage();
16     }

5. Get the auto-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' ) . "','[email protected]')";
        echo $insertUserSql . PHP_EOL;
        $res = $pdo->exec( $insertUserSql );
        echo $pdo->lastInsertId() . PHP_EOL;
    }catch( PDOException $e ) {
        echo $e->getMessage();
    }

6. Execute the 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();
    }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325102357&siteId=291194637