php pdo對象使用詳解: 串連資料庫與exec方法

來源:互聯網
上載者:User

標籤:use   執行   file   cte   參數   var   對象   base   pre   

要使用pdo,首先需要開啟pdo擴充,我這裡已經開啟了mysql的pdo擴充

[email protected]:~$ php -m | grep pdopdo_mysql[email protected]:~$ 

1,串連資料庫

mysql> show create database shop \G;*************************** 1. row ***************************       Database: shopCreate 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: accountCreate 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();    }

>上面為參數形式串連資料庫

>uri形式串連資料庫

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();    }

還有一種是php.ini中寫dsn串連資訊,不太推薦使用

2,exec執行一條sql語句,傳回值為受影響的行數,如果沒有受影響的行數,傳回值為0,該方法對select語句無效

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,執行insert語句

$insertUserSql = "insert into user( username, pwd, email ) values( ‘ghostwu‘," . "‘" . md5( ‘ghostwu‘ )  . "‘" . ",‘[email protected]‘)";        $res = $pdo->exec( $insertUserSql );        var_dump( $res );

4,一次性執行多條sql語句

 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 =<<<EOF10         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,擷取最後一次插入資料的自增id

    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,執行delete語句

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();    }

 

php pdo對象使用詳解: 串連資料庫與exec方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.