如何在php中使用bind_param()函數(shù)?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
for example:
bind_param("sss", firstname,lastname, $email);
1. 該函數(shù)綁定了 SQL 的參數(shù),且告訴數(shù)據(jù)庫參數(shù)的值。 "sss" 參數(shù)列處理其余參數(shù)的數(shù)據(jù)類型。s 字符告訴數(shù)據(jù)庫該參數(shù)為字符串。
參數(shù)有以下四種類型:
i - integer(整型)
d - double(雙精度浮點(diǎn)型)
s - string(字符串)
b - BLOB(布爾值)
每個(gè)參數(shù)都需要指定類型。
通過告訴數(shù)據(jù)庫參數(shù)的數(shù)據(jù)類型,可以降低 SQL 注入的風(fēng)險(xiǎn)。
2. 上面的firstname,lastname, $email傳的是引用,在php5.3之后是不能直接寫成字符串的,為了驗(yàn)證這個(gè)結(jié)論,在此我寫了一段測(cè)試,如下:
$servername="localhost"; $username="root"; $password="admin"; $dbname="test"; $conn=new mysqli($servername,$username,$password,$dbname); if($conn->connect_error){ die("connected failed:".$conn->connect_error); } $sql="INSERT INTO user(user_first,user_last,age)VALUES(?,?,?)"; $stmt=$conn->prepare($sql); $stmt->bind_param("sss","xiao","hong",22); $stmt->execute(); echo "News records created successfully!"; $stmt->close(); $conn->close();
上面我寫了一段將參數(shù)直接寫成字符串的測(cè)試程序,運(yùn)行之后彈出:
最后我將程序改寫為如下:
$servername="localhost"; $username="root"; $password="password"; $dbname="test"; $conn=new mysqli($servername,$username,$password,$dbname); if($conn->connect_error){ die("Connect failed:".$conn->connect_error); } $sql="INSERT INTO user(user_first,user_last,age)VALUES(?,?,?)"; $stmt=$conn->prepare($sql); $stmt->bind_param("sss",$user_first,$user_last,$age); $user_first="xiao"; $user_last="hong"; $age=12; $stmt->execute(); echo "News records created successfully!"; $stmt->close(); $conn->close();
看完上述內(nèi)容,你們掌握如何在php中使用bind_param()函數(shù)的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
當(dāng)前題目:如何在php中使用bind_param()函數(shù)-創(chuàng)新互聯(lián)
當(dāng)前路徑:http://sd-ha.com/article0/shgio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航、自適應(yīng)網(wǎng)站、網(wǎng)站收錄、域名注冊(cè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容