2015-04-02 189 views
3
<?php 

    $db = new PDO($dsn,$username,$password); 
    $uname='avi'; 
    $age=19; 
    $stmt = $db->prepare('INSERT INTO table(uname,age) VALUES(:uname,:age)'); 
    $stmt->execute(array(':uname'=>$uname,':age'=>$age)); 

    $stmt = $db->prepare('INSERT INTO table(uname,age) VALUES(?,?)'); 
    $stmt->execute(array($uname,$age)); 

    $stmt = $db->prepare('INSERT INTO table(uname,age) VALUES(:uname,:age)'); 
    $stmt->bindValue(':uname',$uname); //can be $uname or just 'avi' 
    $stmt->binParam(':age',$uname); //cannot be 'avi' or value only 
    $stmt->execute(); 

?> 

什麼時候應該使用bindParam()?所有以前的方法似乎都比較簡單,並且需要更少的代碼。bindParam(),bindValue()和execute(array())之間有什麼區別和優點

使用bindParam()優於其他方法(bindValue(),​​)有什麼好處?

+0

爲什麼螺絲時,你可以只用指甲?這是完成同樣任務的不同方式。 – 2015-04-02 22:06:04

+0

是的,但大量的網站和一些例子在這裏使用bindParam時應該使用bindParam? SOme exmaple在PHP文檔中顯示它與存儲過程一起使用,返回值爲不同情況下的不同用途 – AAB 2015-04-02 22:08:11

+0

。在完全不起作用的代碼中,bindParam()就像做':foo&= $ var'。 foo參數將是對$ var的引用,並且在調用'execute()'時簡單地將變量中的任何值抽取出來。 ' - > exec(array(...))'版本在那時使用變量值。 – 2015-04-02 22:11:27

回答

4

bindParam()通過引用綁定參數,因此它將在$stmt->execute()處進行評估,這與bindValue()不同,該函數在調用函數本身時進行評估。

因此,作爲一個例子:

bindParam:

<?php 

    try { 

     $dbh = new PDO("mysql:host=localhost;dbname=test", "root", ""); 
     $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

     $stmt = $dbh->prepare("SELECT * FROM test WHERE number = ?"); 
     $stmt->bindParam(1, $xy, PDO::PARAM_INT); 
     $xy = 123; //See here variable is defined after it has been bind 
     $stmt->execute(); 

     print_r($stmt->fetchAll()); 

    } catch(PDOException $e) { 
     echo $e->getMessage(); 
    } 

?> 

的偉大工程!

bindValue:

<?php 

    try { 

     $dbh = new PDO("mysql:host=localhost;dbname=test", "root", ""); 
     $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

     $stmt = $dbh->prepare("SELECT * FROM test WHERE number = ?"); 
     $stmt->bindValue(1, $xy, PDO::PARAM_INT); 
     $xy = 123; //See here variable is defined after it has been bind 
     $stmt->execute(); 

     print_r($stmt->fetchAll()); 

    } catch(PDOException $e) { 
     echo $e->getMessage(); 
    } 

?> 

輸出:

說明:未定義變量:XY

另外一些其他的不同之處:

  • bindParam()還具有它可以(必須)進行,如果調用IN & OUT程序用來存儲輸出回到變量(這也需要追加PDO::PARAM_INPUT_OUTPUT與OR語句類型參數)
  • 隨着bindParam() & bindValue()參數長度你可以指定值,你不能在execute()做的類型,有一切都只是一個字符串(PDO :: PARAM_STR)
+0

嗨,你能解釋它評估什麼嗎?我知道佔位符被替換爲sql語句中的實際值。 – AAB 2015-04-02 22:10:32

+0

@AAB增加了一個例子希望它可以幫助你更好地理解它 – Rizier123 2015-04-02 22:15:49

+0

你的第二個例子,你忘記更改bindParam bindValue – developerwjk 2015-04-02 22:16:17

1

bindParam超過bindValue的好處是,你可以前一個變量綁定你決定放什麼。爲什麼你真的需要這樣做,我不知道,但你可能會這樣做。

綁定值

$x = function_call_to_determine_value(); 
$stmt->bindValue(':x',$x); 
$stmt->execute(); 

綁定帕拉姆

$stmt->bindParam(':x',$x); 
$x = function_call_to_determine_value(); 
$stmt->execute(); 
+0

因此,使用$ stmt = $ db-> prepare('INSERT INTO table(uname,age)VALUES(:uname,:age)'); (數組(':uname'=> $ uname,':age'=> $ age));而不是bindValue好嗎? – AAB 2015-04-02 22:13:01

+0

@AAB通過execute中的數組綁定參數將它們綁定爲字符串...使用bindParam和bindValue,如果需要,您可以指定數據類型。所以不完全一樣。 – developerwjk 2015-04-02 22:14:06

+0

所以execute(array(「age」=> $ age))與bindParam(「:age」,$ age,PARAM_INT)不同,或者你的意思是說我可以指定年齡是bindParam中的一個字符串,如果我想? – AAB 2015-04-02 22:21:26

相關問題