PHP入門教程之使用Mysqli操作數(shù)據(jù)庫的方法(連接,查詢,事務(wù)回滾等)
本文實(shí)例講述了PHP入門教程之使用Mysqli操作數(shù)據(jù)庫的方法。分享給大家供大家參考,具體如下:
Demo1.php
<?php //使用 mysqli 對(duì)象操作數(shù)據(jù)庫 //創(chuàng)建 mysqli 對(duì)象(資源句柄) $_mysqli = new mysqli(); //連接數(shù)據(jù)庫 1.主機(jī)名(ip) 2.賬戶 3.密碼 4.數(shù)據(jù)庫 //mysqli_connect 函數(shù) == $_mysqli -> connect(); $_mysqli -> connect('localhost','root','123456','guest'); //斷開 MySQL mysqli_close() == $_mysqli -> close(); $_mysqli -> close(); ?>
Demo2.php
<?php //不用 connect ,直接使用構(gòu)造方法 $_mysqli = new mysqli('localhost','root','123456','guest'); //單獨(dú)選擇一個(gè)數(shù)據(jù)庫 //這里選擇的數(shù)據(jù)庫會(huì)替代上面的數(shù)據(jù)庫 //為了避免這些麻煩,盡量不用去單獨(dú)指向了 //$_mysqli -> select_db('school'); $_mysqli -> close(); ?>
Demo3.php
<?php header ( 'Content-Type:text/html; charset=utf-8;' ); //連接 mysql //當(dāng)你參數(shù)出現(xiàn)錯(cuò)誤,導(dǎo)致連接錯(cuò)誤的時(shí)候, //$_mysqli 這個(gè)對(duì)象就沒有創(chuàng)建成功,也就是說,沒有資源句柄的功能 //就是沒有調(diào)用 mysqli 下的方法和屬性的能力了 @$_mysqli = new mysqli('localhost','root','123456','guest'); //為什么要用函數(shù)去捕捉呢? //為什么不用面向?qū)ο蟮姆绞饺ゲ蹲侥兀? if(mysqli_connect_errno()){ echo '數(shù)據(jù)庫連接出現(xiàn)了錯(cuò)誤,錯(cuò)誤的信息是:'.mysqli_connect_error(); exit(); } $_mysqli->close(); ?>
Demo4.php
<?php header ( 'Content-Type:text/html; charset=utf-8;' ); //連接 mysql //當(dāng)你參數(shù)出現(xiàn)錯(cuò)誤,導(dǎo)致連接錯(cuò)誤的時(shí)候, //$_mysqli 這個(gè)對(duì)象就沒有創(chuàng)建成功,也就是說,沒有資源句柄的功能 //就是沒有調(diào)用 mysqli 下的方法和屬性的能力了 @$_mysqli = new mysqli('localhost','root','123456','guest'); //為什么要用函數(shù)去捕捉呢? //為什么不用面向?qū)ο蟮姆绞饺ゲ蹲侥兀? if(mysqli_connect_errno()){ echo '數(shù)據(jù)庫連接出現(xiàn)了錯(cuò)誤,錯(cuò)誤的信息是:'.mysqli_connect_error(); exit(); } //$_mysqli -> select_db('fsdfd'); //數(shù)據(jù)庫操作時(shí)發(fā)生的錯(cuò)誤 if($_mysqli -> errno){ echo '數(shù)據(jù)庫操作錯(cuò)誤:'.$_mysqli -> error; } $_mysqli->close(); ?>
Demo5.php
<?php header ( 'Content-Type:text/html; charset=utf-8;' ); $_mysqli = new mysqli('localhost','root','123456','testguest'); //數(shù)據(jù)庫連接時(shí)發(fā)生的錯(cuò)誤 if(mysqli_connect_errno()){ echo '數(shù)據(jù)庫連接出現(xiàn)了錯(cuò)誤,錯(cuò)誤的信息是:'.mysqli_connect_error(); exit(); } //設(shè)置一下編碼 $_mysqli -> set_charset('utf8'); //創(chuàng)建一句 SQL ,獲取數(shù)據(jù)庫的表的數(shù)據(jù) $_sql = "SELECT * FROM tg_user"; //執(zhí)行 SQL 語句,把結(jié)果集賦給 $_result $_result = $_mysqli -> query($_sql); //var_dump($_result); //object(mysqli_result)#2 (0) { } //通過結(jié)果集,我要取得第一行數(shù)據(jù) //fetch_row();是返回的一個(gè)數(shù)組,里面是第一條數(shù)據(jù)的集合 print_r( $_result -> fetch_row()); //運(yùn)行一次,指針下移一條 print_r( $_result -> fetch_row()); //銷毀結(jié)果集 $_result -> free(); $_mysqli->close(); ?>
Demo6.php
<?php header ( 'Content-Type:text/html; charset=utf-8;' ); $_mysqli = new mysqli('localhost','root','123456','testguest'); //數(shù)據(jù)庫連接時(shí)發(fā)生的錯(cuò)誤 if (mysqli_connect_errno()) { echo '數(shù)據(jù)庫連接出現(xiàn)了錯(cuò)誤.錯(cuò)誤的信息是:'.mysqli_connect_error(); exit(); } //設(shè)置一下編碼 $_mysqli->set_charset('utf8'); //創(chuàng)建一句SQL,獲取數(shù)據(jù)庫的表的數(shù)據(jù) $_sql = "SELECT * FROM tg_user"; //創(chuàng)建一個(gè)結(jié)果集 $_result = $_mysqli->query($_sql); //使用索引數(shù)組取值 //print_r($_result->fetch_row()); $_row = $_result->fetch_row(); echo $_row[3]; //遍歷,下標(biāo)很難記[3] while (!!$_row = $_result->fetch_row()) { echo $_row[3].'<br />'; } $_mysqli->close(); ?>
Demo7.php
<?php header ( 'Content-Type:text/html; charset=utf-8;' ); $_mysqli = new mysqli('localhost','root','123456','testguest'); //數(shù)據(jù)庫連接時(shí)發(fā)生的錯(cuò)誤 if (mysqli_connect_errno()) { echo '數(shù)據(jù)庫連接出現(xiàn)了錯(cuò)誤.錯(cuò)誤的信息是:'.mysqli_connect_error(); exit(); } //設(shè)置一下編碼 $_mysqli->set_charset('utf8'); //創(chuàng)建一句SQL,獲取數(shù)據(jù)庫的表的數(shù)據(jù) $_sql = "SELECT * FROM tg_user"; //創(chuàng)建一個(gè)結(jié)果集 $_result = $_mysqli->query($_sql); //使用關(guān)聯(lián)數(shù)組取值 //print_r($_result->fetch_assoc()); $_assoc = $_result->fetch_assoc(); echo $_assoc['tg_username']; //遍歷 while (!!$_assoc = $_result->fetch_assoc()) { echo $_assoc['tg_username'].'<br />'; } $_mysqli->close(); ?>
Demo8.php
<?php header ( 'Content-Type:text/html; charset=utf-8;' ); $_mysqli = new mysqli('localhost','root','123456','testguest'); //數(shù)據(jù)庫連接時(shí)發(fā)生的錯(cuò)誤 if (mysqli_connect_errno()) { echo '數(shù)據(jù)庫連接出現(xiàn)了錯(cuò)誤.錯(cuò)誤的信息是:'.mysqli_connect_error(); exit(); } //設(shè)置一下編碼 $_mysqli->set_charset('utf8'); //創(chuàng)建一句SQL,獲取數(shù)據(jù)庫的表的數(shù)據(jù) $_sql = "SELECT * FROM tg_user"; //創(chuàng)建一個(gè)結(jié)果集 $_result = $_mysqli->query($_sql); //使用索引+關(guān)聯(lián)數(shù)組取值 //print_r($_result->fetch_array()); $_array = $_result->fetch_array(); echo $_array[3]; echo $_array['tg_username']; //遍歷..... $_mysqli->close(); ?>
Demo9.php
<?php header ( 'Content-Type:text/html; charset=utf-8;' ); $_mysqli = new mysqli('localhost','root','123456','testguest'); //數(shù)據(jù)庫連接時(shí)發(fā)生的錯(cuò)誤 if (mysqli_connect_errno()) { echo '數(shù)據(jù)庫連接出現(xiàn)了錯(cuò)誤.錯(cuò)誤的信息是:'.mysqli_connect_error(); exit(); } //設(shè)置一下編碼 $_mysqli->set_charset('utf8'); //創(chuàng)建一句SQL,獲取數(shù)據(jù)庫的表的數(shù)據(jù) $_sql = "SELECT * FROM tg_user"; //創(chuàng)建一個(gè)結(jié)果集 $_result = $_mysqli->query($_sql); //使用OOP的方法object //print_r($_result->fetch_object()); echo $_result->fetch_object()->tg_username; //使用OOP遍歷 while (!!$_object = $_result->fetch_object()) { echo $_object->tg_username.'<br />'; } $_mysqli->close(); ?>
Demo10.php
<?php header ( 'Content-Type:text/html; charset=utf-8;' ); $_mysqli = new mysqli('localhost','root','123456','testguest'); //數(shù)據(jù)庫連接時(shí)發(fā)生的錯(cuò)誤 if (mysqli_connect_errno()) { echo '數(shù)據(jù)庫連接出現(xiàn)了錯(cuò)誤.錯(cuò)誤的信息是:'.mysqli_connect_error(); exit(); } //設(shè)置一下編碼 $_mysqli->set_charset('utf8'); //創(chuàng)建一句SQL,獲取數(shù)據(jù)庫的表的數(shù)據(jù) $_sql = "SELECT * FROM tg_user limit 0,10"; //創(chuàng)建一個(gè)結(jié)果集 $_result = $_mysqli->query($_sql); //我要看下我選擇了多少行 echo $_result->num_rows; //我影響了多少行呢 echo $_mysqli->affected_rows; $_mysqli->close(); ?>
Demo11.php
<?php header ( 'Content-Type:text/html; charset=utf-8;' ); $_mysqli = new mysqli('localhost','root','123456','testguest'); //數(shù)據(jù)庫連接時(shí)發(fā)生的錯(cuò)誤 if (mysqli_connect_errno()) { echo '數(shù)據(jù)庫連接出現(xiàn)了錯(cuò)誤.錯(cuò)誤的信息是:'.mysqli_connect_error(); exit(); } //設(shè)置一下編碼 $_mysqli->set_charset('utf8'); //創(chuàng)建一句SQL,獲取數(shù)據(jù)庫的表的數(shù)據(jù) $_sql = "UPDATE tg_user SET tg_username='一站式建網(wǎng)站' WHERE tg_id=5"; //創(chuàng)建一個(gè)結(jié)果集 $_result = $_mysqli->query($_sql); //我要看下我選擇了多少行 echo $_result->num_rows; echo '|'; //我影響了多少行呢 echo $_mysqli->affected_rows; $_mysqli->close(); ?>
Demo12.php
<?php header ( 'Content-Type:text/html; charset=utf-8;' ); $_mysqli = new mysqli('localhost','root','123456','testguest'); //數(shù)據(jù)庫連接時(shí)發(fā)生的錯(cuò)誤 if (mysqli_connect_errno()) { echo '數(shù)據(jù)庫連接出現(xiàn)了錯(cuò)誤.錯(cuò)誤的信息是:'.mysqli_connect_error(); exit(); } //設(shè)置一下編碼 $_mysqli->set_charset('utf8'); //創(chuàng)建一句SQL,獲取數(shù)據(jù)庫的表的數(shù)據(jù) $_sql = "SELECT * FROM tg_user"; //創(chuàng)建一個(gè)結(jié)果集 $_result = $_mysqli->query($_sql); //求出表中有多少個(gè)字段 echo $_result->field_count; //獲取字段的名字 // $_field = $_result->fetch_field(); // echo $_field->name; // $_field = $_result->fetch_field(); // echo $_field->name; // // while (!!$_field = $_result->fetch_field()) { // echo $_field->name.'<br />'; // } //一次性取得所有的字段 $_fields = $_result->fetch_fields(); //echo $_fields[0]->name; foreach ($_fields as $_field) { echo $_field->name.'<br />'; } $_mysqli->close(); ?>
Demo13.php
<?php header ( 'Content-Type:text/html; charset=utf-8;' ); $_mysqli = new mysqli('localhost','root','123456','testguest'); //數(shù)據(jù)庫連接時(shí)發(fā)生的錯(cuò)誤 if (mysqli_connect_errno()) { echo '數(shù)據(jù)庫連接出現(xiàn)了錯(cuò)誤.錯(cuò)誤的信息是:'.mysqli_connect_error(); exit(); } //設(shè)置一下編碼 $_mysqli->set_charset('utf8'); //創(chuàng)建一句SQL,獲取數(shù)據(jù)庫的表的數(shù)據(jù) $_sql = "SELECT * FROM tg_user"; //創(chuàng)建一個(gè)結(jié)果集 $_result = $_mysqli->query($_sql); //移動(dòng)數(shù)據(jù)指針 $_result->data_seek(9); $_row = $_result->fetch_row(); echo $_row[3]; //移動(dòng)字段指針 $_result->field_seek(3); $_field = $_result->fetch_field(); echo $_field->name; $_mysqli->close(); ?>
Demo14.php
<?php header ( 'Content-Type:text/html; charset=utf-8;' ); $_mysqli = new mysqli('localhost','root','123456','testguest'); //數(shù)據(jù)庫連接時(shí)發(fā)生的錯(cuò)誤 if (mysqli_connect_errno()) { echo '數(shù)據(jù)庫連接出現(xiàn)了錯(cuò)誤.錯(cuò)誤的信息是:'.mysqli_connect_error(); exit(); } //設(shè)置一下編碼 $_mysqli->set_charset('utf8'); //創(chuàng)建三個(gè)修改的SQL語句 $_sql .= "UPDATE tg_article SET tg_username='喀喀喀' WHERE tg_id=1;"; $_sql .= "UPDATE tg_flower SET tg_fromuser='喀喀喀' WHERE tg_id=1;"; $_sql .= "UPDATE tg_friend SET tg_fromuser='喀喀喀' WHERE tg_id=1"; //使用通知執(zhí)行的方法 $_mysqli->multi_query($_sql); //普通只能執(zhí)行sql的方法是:$_mysqli->query($_sql); $_mysqli->close(); ?>
Demo15.php
<?php header ( 'Content-Type:text/html; charset=utf-8;' ); $_mysqli = new mysqli('localhost','root','123456','testguest'); //數(shù)據(jù)庫連接時(shí)發(fā)生的錯(cuò)誤 if (mysqli_connect_errno()) { echo '數(shù)據(jù)庫連接出現(xiàn)了錯(cuò)誤.錯(cuò)誤的信息是:'.mysqli_connect_error(); exit(); } //設(shè)置一下編碼 $_mysqli->set_charset('utf8'); //創(chuàng)建三條選擇語句 $_sql .= "SELECT * FROM tg_photo;"; $_sql .= "SELECT * FROM tg_user;"; $_sql .= "SELECT * FROM tg_friend"; if ($_mysqli->multi_query($_sql)) { //獲取當(dāng)前的結(jié)果集 $_result = $_mysqli->store_result(); print_r($_result->fetch_row()); echo '<br />'; //將結(jié)果集的指針移到下一條 $_mysqli->next_result(); $_result = $_mysqli->store_result(); if (!$_result) { echo '第二條SQL語句有五!'; exit(); } print_r($_result->fetch_row()); echo '<br />'; $_mysqli->next_result(); $_result = $_mysqli->store_result(); if (!$_result) { echo '第三條SQL語句有五!'; exit(); } print_r($_result->fetch_row()); } else { echo '第一條SQL語句有誤'; exit(); } $_mysqli->close(); ?>
Demo16.php
<?php header ( 'Content-Type:text/html; charset=utf-8;' ); $_mysqli = new mysqli('localhost','root','123456','testguest'); //數(shù)據(jù)庫連接時(shí)發(fā)生的錯(cuò)誤 if (mysqli_connect_errno()) { echo '數(shù)據(jù)庫連接出現(xiàn)了錯(cuò)誤.錯(cuò)誤的信息是:'.mysqli_connect_error(); exit(); } //設(shè)置一下編碼 $_mysqli->set_charset('utf8'); //設(shè)置關(guān)閉自動(dòng)提交(手工提交) $_mysqli->autocommit(false); //創(chuàng)建兩個(gè)SQL語句 $_sql .= "UPDATE tg_flower SET tg_flower=tg_flower-50 WHERE tg_id=1;"; $_sql .= "UPDATE tg_friend SET tg_state=tg_state+50 WHERE tg_id=1"; //執(zhí)行多條SQL語句 //只要這兩條SQL語句都成功了,就手工提交給數(shù)據(jù)庫 //否則,就回滾,撤銷之前的有效操作。 if ($_mysqli->multi_query($_sql)) { //通過影響的行數(shù),來判定SQL語句是否成功執(zhí)行 //如果$_success是false說明sql語句有吳,那么就執(zhí)行回滾,否則就手工提交 $_success = $_mysqli->affected_rows == 1 ? true : false; //下移指針 $_mysqli->next_result(); $_success2 = $_mysqli->affected_rows == 1 ? true : false; //如果兩條都成功的話 if ($_success && $_success2) { //執(zhí)行手工提交 $_mysqli->commit(); echo '完美提交'; } else { //執(zhí)行回滾,撤銷之前的所有操作 $_mysqli->rollback(); echo '所有操作歸零!'; } } else { echo '第一條SQL語句有錯(cuò)誤!'; } //再開啟自動(dòng)提交 $_mysqli->autocommit(true); $_mysqli->close(); ?>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+mysqli數(shù)據(jù)庫程序設(shè)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP PDO預(yù)處理語句及事務(wù)的使用
- PHP的PDO預(yù)處理語句與存儲(chǔ)過程
- php_pdo 預(yù)處理語句詳解
- PHP封裝類似thinkphp連貫操作數(shù)據(jù)庫Db類與簡(jiǎn)單應(yīng)用示例
- PHP5中使用mysqli的prepare操作數(shù)據(jù)庫的介紹
- php pdo操作數(shù)據(jù)庫示例
- PHP使用PDO操作數(shù)據(jù)庫的亂碼問題解決方法
- PHP中使用匿名函數(shù)操作數(shù)據(jù)庫的例子
- PHP中的MYSQL常用函數(shù)(php下操作數(shù)據(jù)庫必備)
- php 使用預(yù)處理語句操作數(shù)據(jù)庫
相關(guān)文章
PHP實(shí)現(xiàn)動(dòng)態(tài)壓縮js與css文件的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)動(dòng)態(tài)壓縮js與css文件的方法,涉及php文件讀寫及字符串替換等相關(guān)操作技巧,需要的朋友可以參考下2018-05-05PHP獲取文件擴(kuò)展名的方法實(shí)例總結(jié)
這篇文章主要介紹了PHP獲取文件擴(kuò)展名的方法,結(jié)合實(shí)例形式總結(jié)了6種常用的文件擴(kuò)展名獲取方法,代碼備有較為詳細(xì)的注釋便于理解,需要的朋友可以參考下2017-06-06PHP實(shí)現(xiàn)簡(jiǎn)單實(shí)用的分頁類代碼
這篇文章主要介紹了PHP實(shí)現(xiàn)簡(jiǎn)單實(shí)用的分頁類代碼,結(jié)合實(shí)例形式分析了PHP分頁類的定義與使用技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2016-04-04Zend studio for eclipse中使php可以調(diào)用mysql相關(guān)函數(shù)的設(shè)置方法
默認(rèn)情況zend studio 中的php是不支持mysql 相關(guān)操作,但通過下面的方法即可解決2008-10-10php接口數(shù)據(jù)加密、解密、驗(yàn)證簽名
這篇文章主要介紹了php接口數(shù)據(jù)加密、解密、驗(yàn)證簽名的相關(guān)資料,需要的朋友可以參考下2015-03-03PHP實(shí)現(xiàn)算式驗(yàn)證碼和漢字驗(yàn)證碼實(shí)例
這篇文章主要介紹了PHP實(shí)現(xiàn)算式驗(yàn)證碼和漢字驗(yàn)證碼實(shí)例,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-03-03