標籤:pdo
<?php/*DO->exec() 方法主要是針對沒有結果集合返回的操作,比如 INSERT、UPDATE、DELETE 等操作,它返回的結果是當前操作影響的列數文法:PDO->exec( string statement )*///構造PDO串連header("Content-type:text/html;charset=utf-8");$dbh = "mysql:host=localhost;dbname=test";$db = new PDO($dbh, ‘root‘, ‘123456‘);$db->query("set character set ‘utf8‘");//寫入資料$username="liming";$password = md5("123456");$regdate = time();//返回的是一個整形,所以下面語句中可以不帶引號$sql_exec = "INSERT INTO userlist (username,password,regdate)VALUES(‘$username‘,‘$password‘,$regdate)";$count = $db->exec($sql_exec);echo ‘寫入 ‘.$count.‘ 條資料記錄!‘;echo "<hr/>";$sql_select = "SELECT * FROM userlist";$sth = $db->query($sql_select);//$sth 是結果集對象//$sth->setFetchMode(PDO::FETCH_ASSOC);// 如果不在setFetchMode()中指定返回的結果類型,也可以單獨使用fetch()方法設定while($row = $sth->fetch(PDO::FETCH_ASSOC)){print_r($row); echo "使用者名稱:".$row[‘username‘]." "; echo "密碼:".$row[‘password‘]." "; echo "註冊時間:".date("Y-m-d",$row[‘regdate‘])."  "; }?>
650) this.width=650;" src="http://s4.51cto.com/wyfs02/M01/76/20/wKioL1ZK83iCLuQrAAB3v_fiZN4170.png" title="QQ20151117172749.png" alt="wKioL1ZK83iCLuQrAAB3v_fiZN4170.png" />
650) this.width=650;" src="http://s1.51cto.com/wyfs02/M01/76/22/wKiom1ZK8f3DrbqNAABwHTjplhM584.png" title="QQ20151117172253.png" alt="wKiom1ZK8f3DrbqNAABwHTjplhM584.png" />
650) this.width=650;" src="http://s4.51cto.com/wyfs02/M01/76/22/wKiom1ZK8cXgfnGPAABL3DvPL3I063.png" title="QQ20151117172204.png" alt="wKiom1ZK8cXgfnGPAABL3DvPL3I063.png" />
本文出自 “IT5808” 部落格,請務必保留此出處http://it5808.blog.51cto.com/10842703/1713597
PHP PDO->exec() 執行無返回結果集的 SQL 陳述式