php實現(xiàn)基于PDO的預(yù)處理示例
本文實例講述了php實現(xiàn)基于PDO的預(yù)處理。分享給大家供大家參考,具體如下:
$servername="localhost"; $username="root"; $password="admin"; //$dbname為我的test數(shù)據(jù)庫 $dbname="test"; try{ $conn=new PDO("mysql:host=$servername;dbname=$dbname",$username,$password); //設(shè)置pdo錯誤異常處理 $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); //預(yù)處理SQL并綁定參數(shù) $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); //要執(zhí)行的插入記錄 $user_first="xiao"; $user_last="liang"; $age=15; $stmt->execute(); //要執(zhí)行的插入記錄 $user_first="xiao"; $user_last="cheng"; $age=23; $stmt->execute(); echo "News record created successfully!"; }catch(PDOException $e){ echo "Error:".$e->getMessage(); } $conn=null;
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP基于pdo操作數(shù)據(jù)庫技巧總結(jié)》、《php+Oracle數(shù)據(jù)庫程序設(shè)計技巧總結(jié)》、《PHP+MongoDB數(shù)據(jù)庫操作技巧大全》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
PHP錯誤提示It is not safe to rely on the system……的解決方法
今天小編就為大家分享一篇關(guān)于PHP錯誤提示It is not safe to rely on the system……的解決方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03詳解PHP的Laravel框架中Eloquent對象關(guān)系映射使用
這篇文章主要介紹了PHP的Laravel框架中Eloquent對象關(guān)系映射使用,重點講述了Eloquent的數(shù)據(jù)模型間關(guān)系,需要的朋友可以參考下2016-02-02