-
-
$dbh = new PDO('mysql:host=localhost;dbname=access_control', 'root', '');
- $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $dbh->exec('設定名稱 utf8');
- /*新增*/
- //$sql = "INSERT INTO `user` SET `login`=:login AND `password`=:password";
- $sql = "INSERT INTO `user` (`login` ,`password`)VALUES (:login, :password)"; $stmt = $dbh->準備($sql); $stmt->execute(array(':login'=>'kevin2',':password'=>''));
- echo $dbh->lastinsertid();
- /*修改*/
- $sql = "UPDATE `user` SET `password`=:password WHERE `user_id`=:userId";
- $stmt = $dbh->準備($sql);
- $stmt->execute(array(':userId'=>'7', ':password'=>'4607e782c4d86fd5364d7e4508bb10d9'));
- echo $stmt->rowCount();
- /*刪除*/
- $sql = "DELETE FROM `user` WHERE `login` LIKE 'kevin_'"; //kevin%
- $stmt = $dbh->prepare($sql);
- $stmt->execute();
- echo $stmt->rowCount();
- /*查詢*/
- $login = 'kevin%';
- $sql = "SELECT * FROM `user` WHERE `login` LIKE :login";
- $stmt = $dbh->準備($sql);
- $stmt->execute(array(':login'=>$login));
- while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
- print_r($row);
- }
- print_r( $stmt->fetchAll(PDO::FETCH_ASSOC));
- ?>
複製程式碼
1、建立連接
-
-
$dbh=newPDO('mysql:host=localhost;port=3306; dbname=test',$user,$ pass ,array(
- PDO::ATTR_PERSISTENT=>true
- ));
- ?>
-
複製程式碼
複製程式碼
持久性連結PDO::ATTR_PERSISTENT=>true
2,捕捉錯誤
-
-
-
-
-
-
嘗試{
- $dbh=newPDO('mysql:host=localhost;dbname=test',$user, $dbh=newPDO('mysql:host=localhost;dbname=test',$user, $ $ pass);
- $dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
- $dbh->exec("設定字元集utf8");
- $dbh=null ; // 斷線
- }catch(PDOException$e){
print"Error!:".$e->getMessage()." "; die() ; }?>
複製程式碼
3,pdo事務
-
-
-
-
-
-
嘗試{
- $dbh->setAttribute(PDO::ATTR_ERRMODE,PDOfERRMODE_EXCEPTIONRMODE_ dbh->beginTransaction();//開啟交易
- $dbh->exec("insertintostaff(id,first,last)values(23,'Joe','Bloggs')");
- $dbh- >exec("insertintosalarychange(id,amount,changedate)
- values(23,50000,NOW())");
- $dbh->commit();//提交事務
- }catch(Exception $e){
- $dbh->rollBack();//錯誤回滾
- echo"失敗:".$e->getMessage();
} ?>
複製程式碼
4. 錯誤處理
一。
-
-
-
-
-
嘗試{
- $dbh = new PDO($dsn, $user, $password);
- $sql = '從 CountryCode =:國家的城市中選擇 *';
- $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
- $stmt = $dbh->準備($sql);
- $stmt->bindParam(':country', $country, PDO::PARAM_STR);
- $stmt->execute();
- while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
- print $row['Name'] 。 「/t」;
- }
- } // 如果有問題我們可以在這裡處理
- catch (PDOException $e) {
- echo 'PDO Exception Caught. ';
- echo '資料庫錯誤:
';
- echo 'SQL查詢:', $sql;
echo '錯誤:' 。 } ?>
複製程式碼
1,使用query()
-
-
-
-
-
$dbh->query($sql); 當$sql中變數可以用$dbh->quote($params); //轉義字串的資料
- $sql = 'Select * from city where CountryCode ='.$dbh->quote($country);
- foreach ($dbh->query($sql) as $row) {
- print $row['Name'] 。 “/t”;
- 印出 $row['CountryCode'] 。 “/t”;
印出 $row['Population'] 。 >
複製程式碼
2,使用 prepare, bindParam和 execute [建議用,同時可以用新增、修改、刪除]
-
-
$dbh->prepare($sql); 產生了個PDOStatement物件
- PDOStatement->bindParam()
- PDOStatement->execute();//可以在這裡放綁定的對應變數
- ?>
複製程式碼
3、php pdo交易範例
-
-
try {
- $dbh = new PDO('mysql:host=localhost;dbname=test', 'root' , '');
- $dbh->query('set names utf8;');
- $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $bhd->Transgindaction ();
- $dbh->exec("Insert INTO `test`.`table` (`name` ,`age`)VALUES ('mick', 22);");
- $dbh-> exec("Insert INTO `test`.`table` (`name` ,`age`)VALUES ('lily', 29);");
- $dbh->exec("Insert INTO `test`.` table` (`name` ,`age`)VALUES ('susan', 21);");
- $dbh->commit();
- } catch (Exception $e) {
- $dbh ->rollBack();
- echo "Failed: " . $e->getMessage();
- }
- ?>
複製代碼
|