PDO連接mysql學習筆記

<?php
//PDO連接mysql
//dsn三種寫法: //dsn01
$dsn = 'mysql:host=localhost;dbname=mysql'; //$dsn = 'mysql:host=localhost;dbname=mysql[;port=3306;charset=UTF8]'; /** dsn02 $dsn = 'uri:file://C:\xampp\htdocs\pdo\config.txt'; config.txt :mysql:host=localhost;dbname=mysql linux and windows are feasible $dsn = 'uri:file:///home/pdo/config.txt'; */ /** dsn03 $dsn = 'mydb'; php.ini裏添加 : pdo.dsn.mydb='mysql:host=localhost;dbname=mysql' */ $user = 'root'; $pwd = ''; //若是試圖鏈接到請求的數據庫失敗,則 PDO::__construct() 拋出一個 PDO異常(PDOException) 。 try{ $link = new PDO($dsn,$user,$pwd); }catch(PDOException $e){ echo 'Connection failed:'.$e->getMessage(); } $userSet = $link->query('select * from user'); print_r($userSet);

 

<?PHP

//事務
//開始一個事務,關閉自動提交
 $dbh -> beginTransaction ();

/* do something  */
 
if(true){
    /* 提交更改 */
 $dbh -> commit ();
}else{
     /*  識別出錯誤並回滾更改 */
 $dbh -> rollBack ();
} 
 /* 數據庫鏈接如今返回到自動提交模式 */


/** 注
包括 MySQL 在內的一些數據庫, 當在一個事務內有相似刪除或建立數據表等 DDL 語句時,
會自動致使一個隱式地提交。隱式地提交將沒法回滾此事務範圍內的任何更改。 
更多見http://dev.mysql.com/doc/refman/5.0/en/implicit-commit.html
*/

 

<?php
/**
預處理語句 PDOStatement 類

PDO類返回PDOStatement實例的方法
public PDOStatement prepare  ( string $statement  [, array $driver_options  = array()  ] )
public PDOStatement query  ( string $statement  )
*/

//bindParam 與 bindValue的區別?

/* 使用 INOUT 參數調用一個存儲過程 
使用 PDO::PARAM_* 常量明確地指定參數的類型。
要從一個存儲過程當中返回一個 INOUT 參數,
須要爲 data_type  參數使用按位或操做符去設置 PDO::PARAM_INPUT_OUTPUT 位。 
*/
 $colour  =  'red' ;
 $sth  =  $dbh -> prepare ( 'CALL puree_fruit(?)' );
 $sth -> bindParam ( 1 ,  $colour ,  PDO :: PARAM_STR | PDO :: PARAM_INPUT_OUTPUT ,  12 );
 $sth -> execute ();
 print( "After pureeing fruit, the colour is:  $colour " );

 

<?php

PDOException  extends RuntimeException  {

/* 屬性 */

public array $errorInfo   ;

protected string $message   ;

protected string $code   ;

/* 繼承的方法 */

final public string Exception::getMessage  ( void )

final public Exception Exception::getPrevious  ( void )

final public int Exception::getCode  ( void )

final public string Exception::getFile  ( void )

final public int Exception::getLine  ( void )

final public array Exception::getTrace  ( void )

final public string Exception::getTraceAsString  ( void )

public string Exception::__toString  ( void )

final private void Exception::__clone  ( void )
}
相關文章
相關標籤/搜索