《PHP實例:php實現基于PDO的預處理示例》要點:
本文介紹了PHP實例:php實現基于PDO的預處理示例,希望對您有用。如果有疑問,可以聯系我們。
本文實例講述了php實現基于PDO的預處理.分享給大家供大家參考,具體如下:PHP學習
$servername="localhost"; $username="root"; $password="admin"; //$dbname為我的test數據庫 $dbname="test"; try{ $conn=new PDO("mysql:host=$servername;dbname=$dbname",$username,$password); //設置pdo錯誤異常處理 $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); //預處理SQL并綁定參數 $stmt=$conn->prepare("INSERT INTO user(user_first,user_last,age)VALUES(:user_first,:user_last,:age)"); $stmt->bindParam(":user_first",$user_first); $stmt->bindParam(":user_last", $user_last); $stmt->bindParam(":age",$age); //要執行的插入記錄 $user_first="xiao"; $user_last="liang"; $age=15; $stmt->execute(); //要執行的插入記錄 $user_first="xiao"; $user_last="cheng"; $age=23; $stmt->execute(); echo "News record created successfully!"; }catch(PDOException $e){ echo "Error:".$e->getMessage(); } $conn=null;
更多關于PHP相關內容感興趣的讀者可查看本站專題:《PHP基于pdo操作數據庫技巧總結》、《php+Oracle數據庫程序設計技巧總結》、《PHP+MongoDB數據庫操作技巧大全》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》PHP學習
希望本文所述對大家PHP程序設計有所幫助.PHP學習
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/1309.html