pdo中bindParam()與bindValue的區別

來源:互聯網
上載者:User

標籤:


2012-07-03  ConfiJin  文章來源  閱 2491  轉 2轉藏到我的圖書館分享:
php pdo中bindParam() 和 bindValue()方法的區別

方法 bindParam() 和 bindValue() 非常相似。
唯一的區別就是前者使用一個PHP變數綁定參數,而後者使用一個值。

所以使用bindParam是第二個參數只能用變數名,而不能用變數值,而bindValue至可以使用具體值。

01 $stm = $pdo->prepare("select * from users where user = :user");
02 $user = "jack";
03 //正確
04 $stm->bindParam(":user",$user);
05 //錯誤
06 //$stm->bindParam(":user","jack");
07 //正確
08 $stm->bindValue(":user",$user);
09 //正確
10 $stm->bindValue(":user","jack");

另外在預存程序中,bindParam可以綁定為input/output變數,如下面:

1 $stm = $pdo->prepare("call func(:param1)");
2 $param1 = "abcd";
3 $stm->bindParam(":param1",$param1); //正確
4 $stm->execute();

預存程序執行過後的結果可以直接反應到變數上。

對於那些記憶體中的大資料區塊參數,處於效能的考慮,應優先使用前者。

pdo中bindParam()與bindValue的區別

聯繫我們

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