PDO 對 mysql的基本操作

來源:互聯網
上載者:User

標籤:ctc   pass   ams   lag   dos   cat   country   logs   comm   

PDO擴充操作<?php$dsn = ‘mysql:dbname=yii2;host=localhost‘;$user = ‘root‘;$password = ‘123456‘;try{    $dbh = new PDO($dsn,$user,$password,array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));}catch(PDOException $e){    echo ‘Connection failed: ‘ . $e->getMessage();}//事務使用 - beginTransaction(),commit(),rollBack(),exec()/* // 增加了 new PDO() 中的最後參數try{    $dbh->beginTransaction();    $sqlDel = "delete from country where code = ‘PK‘";    $sqlIn = "insert into country(code,name,pop) values(‘TT‘,‘TEST‘, 9999)";    $dbh->exec($sqlDel);    $dbh->exec($sqlIn);    $dbh->commit();}catch(PDOException $e){    echo "<br />error:<br />";    echo "<pre>";    print_r($e->getMessage());    $dbh->rollBack();}*/// 事務使用 - setAttribute(),beginTransaction(),commit(),rollBack()/*// 設定錯誤模式,一定要設定,不然不會復原與拋出異常,也可以在 new PDO()最後一個參數加這個值$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);try{    $dbh->beginTransaction();     $sqlDel = "delete from country where code = ‘GX‘";     $sqlIn = "insert into country(code,name,population) values(‘PK‘,‘good‘,‘4444444‘)";     $delFlag = $dbh->exec($sqlDel);     $inFlag = $dbh->exec($sqlIn);     var_dump($delFlag);     var_dump($inFlag);     echo ‘ commit ‘;    var_dump($dbh->inTransaction()); // true    $dbh->commit();         var_dump($dbh->lastInsertId());    echo ‘ commit222222 ‘;}catch(PDOException $e){    echo ‘ rollBack ‘;    $dbh->rollBack();    echo $e->getMessage();}$dbh->setAttribute(PDO::ATTR_AUTOCOMMIT,1);*/// 刪除 - exec()/*$sql = "delete from country where code = ‘FK‘";$count = $dbh->exec($sql);var_dump($count); // int(1)   int(0)*///新增 - exec()/*$sql = "insert into country(code,name,population) values(‘FK‘,‘yes‘,13000)";$count = $dbh->exec($sql);var_dump($count); // int(1)*/// 查詢 - query()/*$sql = "select * from country where code =‘AU‘";$res = $dbh->query($sql, PDO::FETCH_ASSOC);foreach($res as $row){    echo "<pre>";    print_r($row);}Array(    [code] => AU    [name] => Australia    [population] => 18886000)*/// 查詢 - fetchAll()/*$sql = "select * from country where code = :code";$sth = $dbh->prepare($sql);$sth->execute(array(":code"=>"AU"));$res = $sth->fetchAll(PDO::FETCH_ASSOC); // 也可以用在 $dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC),設定只關聯陣列print_r($res);*//*Array(    [0] => Array        (            [code] => AU            [0] => AU            [name] => Australia            [1] => Australia            [population] => 18886000            [2] => 18886000        ))Array(    [0] => Array        (            [code] => AU            [name] => Australia            [population] => 18886000        ))*/// PDOStatement 操作<?php // http://php.net/manual/zh/pdostatement.execute.php$dsn = ‘mysql:host=localhost;dbname=yii2‘;$username = ‘root‘;$password = ‘123456‘;try{    $dbh = new PDO($dsn,$username,$password);}catch(PDOException $e){    echo "failure : ";    echo $e->getMessage();    exit();}echo "<pre>";/* 列印一條SQL預先處理命令 - debugDumpParams$name = ‘GD‘;$sql = "select * from country where name = :name";$res = $dbh->prepare($sql);$res->bindValue(":name", $name);$res->execute();$res->debugDumpParams();$rr = $res->fetchAll(PDO::FETCH_ASSOC);print_r($rr);SQL: [40] select * from country where name = :nameParams:  1Key: Name: [5] :nameparamno=-1name=[5] ":name"is_param=1param_type=2*//* 擷取記錄的列數 columnCount()$sql = "select * from country";$res = $dbh->prepare($sql);$res->execute();$rr = $res->columnCount();print_r($rr); // 3*//* 返回受影響的行數 - rowCount(), prepare(),bindValue(),execute(),$code = ‘PK‘;$sql = "update country set name = ‘GD‘ where code = :code";$res = $dbh->prepare($sql);$res->bindValue(":code", $code);$res->execute();$affectCount = $res->rowCount();print_r($affectCount); // 1*//* 查詢 - prepare(),bindValue(),fetchAll(),execute()$name = ‘good‘;$sql = "select count(1) as total from country where name = :name";$res = $dbh->prepare($sql);$res->bindValue(":name",$name);$res->execute();$rr = $res->fetchAll(PDO::FETCH_ASSOC);print_r($rr);Array(    [0] => Array        (            [total] => 2        ))*//* 查詢 - bindValue(),execute(),fetchAll()$name = ‘good‘;$code = ‘FK‘;$sql = "select * from country where name = ? and code = ? limit 1";$res = $dbh->prepare($sql);$res->bindValue(1,$name);$res->bindValue(2,$code);$res->execute();$rr = $res->fetchAll(PDO::FETCH_ASSOC);print_r($rr);Array(    [0] => Array        (            [code] => FK            [name] => good            [population] => 4444444        ))*//* 查詢 - prepare(),bindValue(),execute(),fetchAll()$name = "good";$code = ‘FK‘;$sql = "select * from country where name = :name and code = :code limit 1";$res = $dbh->prepare($sql);$res->bindValue(":code", $code);$res->bindValue(":name", $name);$res->execute();$rr = $res->fetchAll();print_r($rr);Array(    [0] => Array        (            [code] => FK            [0] => FK            [name] => good            [1] => good            [population] => 4444444            [2] => 4444444        ))*//* 查詢 - prepare(),bindParam(),execute(),fetchAll()$name = ‘good‘;$code = ‘PK‘;$sql = "select * from country where name = ? and code = ?";$res = $dbh->prepare($sql);$res->bindParam(1, $name);$res->bindParam(2, $code);$res->execute();$rr = $res->fetchAll(PDO::FETCH_ASSOC);print_r($rr);Array(    [0] => Array        (            [code] => PK            [name] => good            [population] => 4444444        ))*/// 查詢 - prepare(),bindParam(),execute(),fetchAll()/*$name = ‘good‘;$code = ‘PK‘;$population = 4444444;$sql = "select * from country where name = :name and code = :code and population = :population";$res = $dbh->prepare($sql);$res->bindParam(":code", $code);$res->bindParam(":name", $name,PDO::PARAM_STR);$res->bindParam(":population", $population);$res->execute();$rr = $res->fetchAll(PDO::FETCH_ASSOC);print_r($rr);Array(    [0] => Array        (            [code] => PK            [name] => good            [population] => 4444444        ))*/// 查詢 - prepare(),execute(),fetch()/*$sql = "select * from country limit 2";$res = $dbh->prepare($sql);$res->execute();while($rs = $res->fetch(PDO::FETCH_ASSOC)){    print_r($rs);}Array(    [code] => AU    [name] => Australia    [population] => 18886000)Array(    [code] => BR    [name] => Brazil    [population] => 170115000)*/// 查詢 - prepare(),execute(),fetchAll()/*$sql = "select * from country limit 1";$res = $dbh->prepare($sql);$res->execute();$rr = $res->fetchAll();print_r($rr); Array(    [0] => Array        (            [code] => AU            [0] => AU            [name] => Australia            [1] => Australia            [population] => 18886000            [2] => 18886000        ))*/// 查詢 - prepare(),execute(),fetchAll()/**$sql = "select * from country limit 1";$sth = $dbh->prepare($sql);$sth->execute();$res = $sth->fetchAll(PDO::FETCH_ASSOC);echo "<pre>";print_r($res);Array(    [0] => Array        (            [code] => AU            [name] => Australia            [population] => 18886000        ))*/

 

PDO 對 mysql的基本操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.