php資料庫操作--資料預先處理、更新、刪除__資料庫

來源:互聯網
上載者:User

終於又開始繼續寫部落格了,離回家不遠了 不知道大家的票有沒有買下,希望大家都可以回家過個團圓年。

本文簡單講述“如題”

語句預先處理:通俗的就是一次查詢,多次執行,在我們後期的項目中會經常用到

建立:

//建立預先處理$createinto=$connent->prepare("insert into zh(name,age,email) values (?,?,?)");

sql語句,參數使用?代替為預留

//綁定$createinto->bind_param("sis",$name,$age,$email);

綁定參數 s為String類型 i為int類型

$name="zhanghao1";$age=1;$email="1234123123@qq.com";$createinto->execute();$name="zhanghao2";$age=2;$email="1234123123@qq.com";$createinto->execute();
執行語句;最後資料插入成功。(前提是串連到資料庫並使用)



刪除指定條目:

mysqli_query($connent,"delete from zh where name='zhanghao1'");
不加where條件刪除整個表資料

更新指定條目:

mysqli_query($connent,"update zh set age=3 where name='zhanghao2'");
修改zhanghao2的年齡為3

全部資料庫操作完之後要關閉資料庫。

----完整代碼-------

<?php/* * 資料預先處理  刪除 更新資料 * * To change the template for this generated file go to * Window - Preferences - PHPeclipse - PHP - Code Templates */ /**  * 預先處理 簡單化  建立一種方法 多處使用  *///先串連資料庫$servername="localhost";$username="root";$userpassword="hao542161";$dbname = "testdb";$connent=new mysqli($servername,$username,$userpassword,$dbname);if($connent->connect_error){die("串連失敗: " . $connent->connect_error);}else{echo "成功";}//建立預先處理$createinto=$connent->prepare("insert into zh(name,age,email) values (?,?,?)");//綁定$createinto->bind_param("sis",$name,$age,$email);//多次執行$name="zhanghao1";$age=1;$email="1234123123@qq.com";$createinto->execute();$name="zhanghao2";$age=2;$email="1234123123@qq.com";$createinto->execute();echo "插入成功";//刪除資料 刪除表中 name為zhanghao1的資料mysqli_query($connent,"delete from zh where name='zhanghao1'");mysqli_query($connent,"update zh set age=3 where name='zhanghao2'");$connent->close();?>


我們可以發現其中where是判斷條件的根本,根據他我們可以條件查詢,條件刪除和條件修改。




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.