您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)PHP如何使用Mysqli操作數(shù)據(jù)庫(kù)的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
具體如下:
Demo1.php
<?php //使用 mysqli 對(duì)象操作數(shù)據(jù)庫(kù) //創(chuàng)建 mysqli 對(duì)象(資源句柄) $_mysqli = new mysqli(); //連接數(shù)據(jù)庫(kù) 1.主機(jī)名(ip) 2.賬戶 3.密碼 4.數(shù)據(jù)庫(kù) //mysqli_connect 函數(shù) == $_mysqli -> connect(); $_mysqli -> connect('localhost','root','123456','guest'); //斷開(kāi) MySQL mysqli_close() == $_mysqli -> close(); $_mysqli -> close(); ?>
Demo2.php
<?php //不用 connect ,直接使用構(gòu)造方法 $_mysqli = new mysqli('localhost','root','123456','guest'); //單獨(dú)選擇一個(gè)數(shù)據(jù)庫(kù) //這里選擇的數(shù)據(jù)庫(kù)會(huì)替代上面的數(shù)據(jù)庫(kù) //為了避免這些麻煩,盡量不用去單獨(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ì)象就沒(méi)有創(chuàng)建成功,也就是說(shuō),沒(méi)有資源句柄的功能 //就是沒(méi)有調(diào)用 mysqli 下的方法和屬性的能力了 @$_mysqli = new mysqli('localhost','root','123456','guest'); //為什么要用函數(shù)去捕捉呢? //為什么不用面向?qū)ο蟮姆绞饺ゲ蹲侥兀? if(mysqli_connect_errno()){ echo '數(shù)據(jù)庫(kù)連接出現(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ì)象就沒(méi)有創(chuàng)建成功,也就是說(shuō),沒(méi)有資源句柄的功能 //就是沒(méi)有調(diào)用 mysqli 下的方法和屬性的能力了 @$_mysqli = new mysqli('localhost','root','123456','guest'); //為什么要用函數(shù)去捕捉呢? //為什么不用面向?qū)ο蟮姆绞饺ゲ蹲侥兀? if(mysqli_connect_errno()){ echo '數(shù)據(jù)庫(kù)連接出現(xiàn)了錯(cuò)誤,錯(cuò)誤的信息是:'.mysqli_connect_error(); exit(); } //$_mysqli -> select_db('fsdfd'); //數(shù)據(jù)庫(kù)操作時(shí)發(fā)生的錯(cuò)誤 if($_mysqli -> errno){ echo '數(shù)據(jù)庫(kù)操作錯(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ù)庫(kù)連接時(shí)發(fā)生的錯(cuò)誤 if(mysqli_connect_errno()){ echo '數(shù)據(jù)庫(kù)連接出現(xiàn)了錯(cuò)誤,錯(cuò)誤的信息是:'.mysqli_connect_error(); exit(); } //設(shè)置一下編碼 $_mysqli -> set_charset('utf8'); //創(chuàng)建一句 SQL ,獲取數(shù)據(jù)庫(kù)的表的數(shù)據(jù) $_sql = "SELECT * FROM tg_user"; //執(zhí)行 SQL 語(yǔ)句,把結(jié)果集賦給 $_result $_result = $_mysqli -> query($_sql); //var_dump($_result); //object(mysqli_result)#2 (0) { } //通過(guò)結(jié)果集,我要取得第一行數(shù)據(jù) //fetch_row();是返回的一個(gè)數(shù)組,里面是第一條數(shù)據(jù)的集合 print_r( $_result -> fetch_row()); //運(yùn)行一次,指針下移一條 print_r( $_result -> fetch_row()); //銷(xiāo)毀結(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ù)庫(kù)連接時(shí)發(fā)生的錯(cuò)誤 if (mysqli_connect_errno()) { echo '數(shù)據(jù)庫(kù)連接出現(xiàn)了錯(cuò)誤.錯(cuò)誤的信息是:'.mysqli_connect_error(); exit(); } //設(shè)置一下編碼 $_mysqli->set_charset('utf8'); //創(chuàng)建一句SQL,獲取數(shù)據(jù)庫(kù)的表的數(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ù)庫(kù)連接時(shí)發(fā)生的錯(cuò)誤 if (mysqli_connect_errno()) { echo '數(shù)據(jù)庫(kù)連接出現(xiàn)了錯(cuò)誤.錯(cuò)誤的信息是:'.mysqli_connect_error(); exit(); } //設(shè)置一下編碼 $_mysqli->set_charset('utf8'); //創(chuàng)建一句SQL,獲取數(shù)據(jù)庫(kù)的表的數(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ù)庫(kù)連接時(shí)發(fā)生的錯(cuò)誤 if (mysqli_connect_errno()) { echo '數(shù)據(jù)庫(kù)連接出現(xiàn)了錯(cuò)誤.錯(cuò)誤的信息是:'.mysqli_connect_error(); exit(); } //設(shè)置一下編碼 $_mysqli->set_charset('utf8'); //創(chuàng)建一句SQL,獲取數(shù)據(jù)庫(kù)的表的數(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ù)庫(kù)連接時(shí)發(fā)生的錯(cuò)誤 if (mysqli_connect_errno()) { echo '數(shù)據(jù)庫(kù)連接出現(xiàn)了錯(cuò)誤.錯(cuò)誤的信息是:'.mysqli_connect_error(); exit(); } //設(shè)置一下編碼 $_mysqli->set_charset('utf8'); //創(chuàng)建一句SQL,獲取數(shù)據(jù)庫(kù)的表的數(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ù)庫(kù)連接時(shí)發(fā)生的錯(cuò)誤 if (mysqli_connect_errno()) { echo '數(shù)據(jù)庫(kù)連接出現(xiàn)了錯(cuò)誤.錯(cuò)誤的信息是:'.mysqli_connect_error(); exit(); } //設(shè)置一下編碼 $_mysqli->set_charset('utf8'); //創(chuàng)建一句SQL,獲取數(shù)據(jù)庫(kù)的表的數(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ù)庫(kù)連接時(shí)發(fā)生的錯(cuò)誤 if (mysqli_connect_errno()) { echo '數(shù)據(jù)庫(kù)連接出現(xiàn)了錯(cuò)誤.錯(cuò)誤的信息是:'.mysqli_connect_error(); exit(); } //設(shè)置一下編碼 $_mysqli->set_charset('utf8'); //創(chuàng)建一句SQL,獲取數(shù)據(jù)庫(kù)的表的數(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ù)庫(kù)連接時(shí)發(fā)生的錯(cuò)誤 if (mysqli_connect_errno()) { echo '數(shù)據(jù)庫(kù)連接出現(xiàn)了錯(cuò)誤.錯(cuò)誤的信息是:'.mysqli_connect_error(); exit(); } //設(shè)置一下編碼 $_mysqli->set_charset('utf8'); //創(chuàng)建一句SQL,獲取數(shù)據(jù)庫(kù)的表的數(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ù)庫(kù)連接時(shí)發(fā)生的錯(cuò)誤 if (mysqli_connect_errno()) { echo '數(shù)據(jù)庫(kù)連接出現(xiàn)了錯(cuò)誤.錯(cuò)誤的信息是:'.mysqli_connect_error(); exit(); } //設(shè)置一下編碼 $_mysqli->set_charset('utf8'); //創(chuàng)建一句SQL,獲取數(shù)據(jù)庫(kù)的表的數(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ù)庫(kù)連接時(shí)發(fā)生的錯(cuò)誤 if (mysqli_connect_errno()) { echo '數(shù)據(jù)庫(kù)連接出現(xiàn)了錯(cuò)誤.錯(cuò)誤的信息是:'.mysqli_connect_error(); exit(); } //設(shè)置一下編碼 $_mysqli->set_charset('utf8'); //創(chuàng)建三個(gè)修改的SQL語(yǔ)句 $_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ù)庫(kù)連接時(shí)發(fā)生的錯(cuò)誤 if (mysqli_connect_errno()) { echo '數(shù)據(jù)庫(kù)連接出現(xiàn)了錯(cuò)誤.錯(cuò)誤的信息是:'.mysqli_connect_error(); exit(); } //設(shè)置一下編碼 $_mysqli->set_charset('utf8'); //創(chuàng)建三條選擇語(yǔ)句 $_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語(yǔ)句有五!'; exit(); } print_r($_result->fetch_row()); echo '<br />'; $_mysqli->next_result(); $_result = $_mysqli->store_result(); if (!$_result) { echo '第三條SQL語(yǔ)句有五!'; exit(); } print_r($_result->fetch_row()); } else { echo '第一條SQL語(yǔ)句有誤'; exit(); } $_mysqli->close(); ?>
Demo16.php
<?php header ( 'Content-Type:text/html; charset=utf-8;' ); $_mysqli = new mysqli('localhost','root','123456','testguest'); //數(shù)據(jù)庫(kù)連接時(shí)發(fā)生的錯(cuò)誤 if (mysqli_connect_errno()) { echo '數(shù)據(jù)庫(kù)連接出現(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語(yǔ)句 $_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語(yǔ)句 //只要這兩條SQL語(yǔ)句都成功了,就手工提交給數(shù)據(jù)庫(kù) //否則,就回滾,撤銷(xiāo)之前的有效操作。 if ($_mysqli->multi_query($_sql)) { //通過(guò)影響的行數(shù),來(lái)判定SQL語(yǔ)句是否成功執(zhí)行 //如果$_success是false說(shuō)明sql語(yǔ)句有吳,那么就執(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í)行回滾,撤銷(xiāo)之前的所有操作 $_mysqli->rollback(); echo '所有操作歸零!'; } } else { echo '第一條SQL語(yǔ)句有錯(cuò)誤!'; } //再開(kāi)啟自動(dòng)提交 $_mysqli->autocommit(true); $_mysqli->close(); ?>
感謝各位的閱讀!關(guān)于“PHP如何使用Mysqli操作數(shù)據(jù)庫(kù)”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。