PHP中PDO DEMO

來源:互聯網
上載者:User

標籤:style   blog   color   os   strong   for   ar   div   代碼   

PDO =》 PHP DATABASE OBJECT

1、Select

$dsn = "mysql:host=127.0.0.1;port=3306;dbname=dbname"; $dbh = new PDO($dsn, ‘root‘, ‘password‘);$sql = "select * from table_name where NAME = :name AND PWD = :pwd";$sth = $dbh->prepare($sql);$sth ->bindValue(‘:name‘, ‘user‘);$sth ->bindValue(‘:pwd‘, ‘password‘);$sth-> execute(); foreach($sth as $row) {     echo var_dump($row); } $dbh = null;
$dsn = "mysql:host=127.0.0.1;port=3306;dbname=dbname"; $dbh = new PDO($dsn, ‘root‘, ‘password‘);$sql = "select * from table_name where NAME = ?AND PWD = ?";$sth = $dbh->prepare($sql);$sth ->bindValue(1, ‘user‘);$sth ->bindValue(2, ‘password‘);$sth-> execute(); foreach($sth as $row) {     echo var_dump($row); } $dbh = null;

2、UPDATE

$dsn = "mysql:host=127.0.0.1;port=3306;dbname=dbname"; $dbh = new PDO($dsn, ‘root‘, ‘password‘);$sql = "update table_name set name = :name where id = :id";$sth = $dbh->prepare($sql);$sth ->bindValue(‘:name‘, ‘user‘);$sth ->bindValue(‘:id‘, ‘1‘);$flag = $sth-> execute(); // true or false$dbh = null;
$dsn = "mysql:host=127.0.0.1;port=3306;dbname=dbname"; $dbh = new PDO($dsn, ‘root‘, ‘password‘);$sql = "update table_name set name = ? where id = ?";$sth = $dbh->prepare($sql);$sth ->bindValue(1, ‘user‘);$sth ->bindValue(2, ‘1‘);$flag = $sth-> execute(); // true or false$dbh = null;

 3、Insert

$dsn = "mysql:host=127.0.0.1;port=3306;dbname=dbname"; $dbh = new PDO($dsn, ‘root‘, ‘password‘);$sql = "insert into table_name (name) values (:name)";$sth = $dbh->prepare($sql);$sth ->bindValue(‘:name‘, ‘user‘);$flag = $sth-> execute(); // true or false$dbh = null;
$dsn = "mysql:host=127.0.0.1;port=3306;dbname=dbname"; $dbh = new PDO($dsn, ‘root‘, ‘password‘);$sql = "insert into table_name (name) values (?)";$sth = $dbh->prepare($sql);$sth ->bindValue(1, ‘user‘);$flag = $sth-> execute(); // true or false$dbh = null;

4、Delete

$dsn = "mysql:host=127.0.0.1;port=3306;dbname=dbname"; $dbh = new PDO($dsn, ‘root‘, ‘password‘);$sql = "delete from table_name where id = :id";$sth = $dbh->prepare($sql);$sth ->bindValue(‘:id‘, ‘1‘);$flag = $sth-> execute(); // true or false$dbh = null;
$dsn = "mysql:host=127.0.0.1;port=3306;dbname=dbname"; $dbh = new PDO($dsn, ‘root‘, ‘password‘);$sql = "delete from table_name where id = ?";$sth = $dbh->prepare($sql);$sth ->bindValue(1, ‘1‘);$flag = $sth-> execute(); // true or false$dbh = null;

 

每一部分的第二段代碼都是用?和數字索引的方式來綁定參數,有的人可能不是很理解這些後綁定跟直接產生sql語句之後去執行有什麼差別,其實不難理解。

假設你輸入:

select * from table_name where id = ?

問號的部分如果直接動態產生就可能變成這樣

select * from table_name where id = 1 or 1=1

而如果動態綁定的話,問號部分就被限制只能輸入一個跟id欄位類型相符合的變數,如果有sql注入就會編譯不過

PHP中PDO DEMO

聯繫我們

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