亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 65
粉絲 3
評論 4
訪問量 79704
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
mysqli之預(yù)處理
無恥的魚
原創(chuàng)
1277人瀏覽過

預(yù)處理的好處

    • 1.防止SQL注入

    • 2.一條語句多次使用

預(yù)處理步驟

    準(zhǔn)備工作:連接數(shù)據(jù)庫,準(zhǔn)備SQL語句

    1. 創(chuàng)建預(yù)處理對象

    2. 檢測SQL語句

    3. 參數(shù)綁定

    4. 執(zhí)行操作

    5. 銷毀對象

    6. 關(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í)例放上來瞅瞅

查的實(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);

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(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);

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(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);

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(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);

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例


批改狀態(tài):合格

老師批語:
本博文版權(quán)歸博主所有,轉(zhuǎn)載請注明地址!如有侵權(quán)、違法,請聯(lián)系admin@php.cn舉報(bào)處理!
全部評論 文明上網(wǎng)理性發(fā)言,請遵守新聞評論服務(wù)協(xié)議
0條評論
作者最新博文
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(guān)注服務(wù)號 技術(shù)交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)