預(yù)處理的好處
1.防止SQL注入
2.一條語句多次使用
預(yù)處理步驟
準(zhǔn)備工作:連接數(shù)據(jù)庫,準(zhǔn)備SQL語句
創(chuàng)建預(yù)處理對象
檢測SQL語句
參數(shù)綁定
執(zhí)行操作
銷毀對象
關(guān)閉數(shù)據(jù)庫連接
預(yù)處理的增刪改查示例
其實(shí)呢,預(yù)處理的增刪改查基本操作都一樣
下面呢 我把他們一樣的拿出來,
基本樣式
1連接數(shù)據(jù)庫,準(zhǔn)備SQL語句 sql語句中的值必須為問號,且順序不能亂
require 'connect.php'; $sql = "INSERT IGNORE aaa SET name=?,age=?,money=?;"; // 給變量賦值 $name= '西施妹妹'; $age=16; $money=5600;
創(chuàng)建預(yù)處理對象/檢測SQL語句---這兩句是公共樣式
//創(chuàng)建stmt預(yù)處理對象 $stmt = mysqli_stmt_init($db); //檢測SQL語句 //mysqli_stmt_prepare() if(mysqli_stmt_prepare($stmt,$sql)){
參數(shù)綁定--參數(shù)就是根據(jù)你要與處理得參數(shù)來定,注意一下他們的類型
//講變量與SQL語句的占位符進(jìn)行綁定 // mysqli_stmt_bind_param() mysqli_stmt_bind_param($stmt,"sii",$name,$age,$money);
執(zhí)行操作 ----增刪改的執(zhí)行是一樣的,查的不一樣,繼續(xù)往下看 ,下邊上實(shí)例
// 執(zhí)行SQL語句 // mysqli_stmt_execute() mysqli_stmt_execute($stmt); //更新條數(shù) echo '新增了'.mysqli_stmt_affected_rows($stmt).'條,ID是:'.mysqli_stmt_insert_id($stmt);
銷毀對象/關(guān)閉數(shù)據(jù)庫連接---公共樣式
} //關(guān)閉 mysqli_stmt_close($stmt); mysqli_close($db);
光這樣看也看不出個(gè)啥 下面把增刪改查的實(shí)例放上來瞅瞅
<?php /** * User: Z先生 * Date: 2018/4/25 * 數(shù)據(jù)查詢 */ require 'connect.php'; $sql = "SELECT name,age FROM aaa WHERE id=?;"; // 給變量賦值 $id = 34; //創(chuàng)建stmt預(yù)處理對象 $stmt = mysqli_stmt_init($db); //檢測SQL語句 //mysqli_stmt_prepare() if(mysqli_stmt_prepare($stmt,$sql)){ //講變量與SQL語句的占位符進(jìn)行綁定 // mysqli_stmt_bind_param() mysqli_stmt_bind_param($stmt,"i",$id); // 執(zhí)行SQL語句 // mysqli_stmt_execute() mysqli_stmt_execute($stmt); // 綁定結(jié)果變量 SQL查了幾個(gè)字段這里就設(shè)置幾個(gè)字段 mysqli_stmt_bind_result($stmt,$name,$age); // 獲取值 mysqli_stmt_fetch($stmt); // 輸出 echo $name .'的年齡是'. $age; } //關(guān)閉 mysqli_stmt_close($stmt); mysqli_close($db);
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
<?php /** * User: Z先生 * Date: 2018/4/25 * 數(shù)據(jù)查詢 */ require 'connect.php'; $sql = "INSERT IGNORE aaa SET name=?,age=?,money=?;"; // 給變量賦值 $name= '西施妹妹'; $age=16; $money=5600; //創(chuàng)建stmt預(yù)處理對象 $stmt = mysqli_stmt_init($db); //檢測SQL語句 //mysqli_stmt_prepare() if(mysqli_stmt_prepare($stmt,$sql)){ //講變量與SQL語句的占位符進(jìn)行綁定 // mysqli_stmt_bind_param() mysqli_stmt_bind_param($stmt,"sii",$name,$age,$money); // 執(zhí)行SQL語句 // mysqli_stmt_execute() mysqli_stmt_execute($stmt); //更新條數(shù) echo '新增了'.mysqli_stmt_affected_rows($stmt).'條,ID是:'.mysqli_stmt_insert_id($stmt); } //關(guān)閉 mysqli_stmt_close($stmt); mysqli_close($db);
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
<?php /** * User: Z先生 * Date: 2018/4/25 * 數(shù)據(jù)查詢 */ require 'connect.php'; $sql = "DELETE FROM aaa WHERE id=?;"; // 給變量賦值 $id = 34; //創(chuàng)建stmt預(yù)處理對象 $stmt = mysqli_stmt_init($db); //檢測SQL語句 //mysqli_stmt_prepare() if(mysqli_stmt_prepare($stmt,$sql)){ //講變量與SQL語句的占位符進(jìn)行綁定 // mysqli_stmt_bind_param() mysqli_stmt_bind_param($stmt,"i",$id); // 執(zhí)行SQL語句 // mysqli_stmt_execute() mysqli_stmt_execute($stmt); //更新條數(shù) echo '刪除了'.mysqli_stmt_affected_rows($stmt).'條'; } //關(guān)閉 mysqli_stmt_close($stmt); mysqli_close($db);
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
<?php /** * User: Z先生 * Date: 2018/4/25 * 數(shù)據(jù)查詢 */ require 'connect.php'; $sql = "UPDATE aaa SET name=? WHERE id=?;"; // 給變量賦值 $name = "劉大姐"; $id = 34; //創(chuàng)建stmt預(yù)處理對象 $stmt = mysqli_stmt_init($db); //檢測SQL語句 //mysqli_stmt_prepare() if(mysqli_stmt_prepare($stmt,$sql)){ //講變量與SQL語句的占位符進(jìn)行綁定 // mysqli_stmt_bind_param() mysqli_stmt_bind_param($stmt,"si",$name,$id); // 執(zhí)行SQL語句 // mysqli_stmt_execute() mysqli_stmt_execute($stmt); //更新條數(shù) echo '更新了'.mysqli_stmt_affected_rows($stmt).'條'; } //關(guān)閉 mysqli_stmt_close($stmt); mysqli_close($db);
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號