PHP mysqli擴充庫 預先處理技術

來源:互聯網
上載者:User

1、使用mysqli擴充庫 預先處理技術 mysqli stmt 向資料庫添加3個使用者

<?php    //mysqli擴充庫 預先處理技術 mysqli stmt 向資料庫添加3個使用者    //1、建立mysqli對象    $mysqli = new MySQLi("localhost","root","root","test");    if($mysqli->connect_error){        die($mysqli->conncet_error);    }    //2、建立先行編譯對象    $sql="insert into user1(name,password,email,age) values(?,?,?,?)";    $mysqli_stmt=$mysqli->prepare($sql);    //綁定參數    $name="小芳";
  //$password=md5("ffff"); $password="123456"; $email="xiaofang@126.com"; $age=18; //參數綁定->給?號賦值 這裡類型和順序要一致 $mysqli_stmt->bind_param("sssi",$name,$password,$email,$age); //執行 $b=$mysqli_stmt->execute(); //繼續添加 $name="小楊"; $password="123456"; $email="xiaoyang@126.com"; $age=18; //參數綁定->給?號賦值 這裡類型和順序要一致 $mysqli_stmt->bind_param("sssi",$name,$password,$email,$age); //執行 $b=$mysqli_stmt->execute(); //繼續添加 $name="小G"; $password="123456"; $email="xiaoG@126.com"; $age=18; //參數綁定->給?號賦值 這裡類型和順序要一致 $mysqli_stmt->bind_param("sssi",$name,$password,$email,$age); //執行 $b=$mysqli_stmt->execute(); if(!$b){ echo "操作失敗".$mysqli_stmt->error; }else{ echo "操作成功"; } //關閉先行編譯 $mysqli_stmt->close(); $mysqli->close();?>

2、使用預先處理查詢id>5的使用者id name email

<?php    //使用預先處理查詢id>5的使用者id name email    $mysqli=new MySQLi("localhost","root","root","test");    if($mysqli->connect_error){        die($mysqli->connect_error);    }    //建立先行編譯對象    $sql="select id,name,email from user1 where id>?";    $mysqli_stmt=$mysqli->prepare($sql);        $id=5;    //綁定參數    $mysqli_stmt->bind_param("i",$id);    //綁定結果集    $mysqli_stmt->bind_result($id,$name,$email);    //執行    $mysqli_stmt->execute();    //取出綁定的值    while($mysqli_stmt->fetch()){        echo "<br/>$id--$name--$email";    }        //關閉資源    //釋放結果    $mysqli_stmt->free_result();    //關閉與編譯語句    $mysqli_stmt->close();    //關閉串連    $mysqli->close();?>

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.