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)文章
如何解決CI框架的Disallowed Key Characters錯(cuò)誤提示
本篇文章是對(duì)解決CodeIgniter框架應(yīng)用中,出現(xiàn)Disallowed Key Characters錯(cuò)誤提示的方法,進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以參考下2013-07-07
簡(jiǎn)單談?wù)刾hp中ob_flush和flush的區(qū)別
本文簡(jiǎn)單的對(duì)php中ob_flush和flush進(jìn)行了對(duì)比分析,得出了他們之間的區(qū)別,給需要的小伙伴參考下。2014-11-11
jQuery+PHP實(shí)現(xiàn)圖片上傳并提交功能
這篇文章主要介紹了jQuery加PHP實(shí)現(xiàn)圖片上傳并提交實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
有關(guān)phpmailer的詳細(xì)介紹及使用方法
有關(guān)phpmailer的用法,有需要的朋友不妨參考下2013-01-01
PHP Swoole異步Redis客戶端實(shí)現(xiàn)方法示例
這篇文章主要介紹了PHP Swoole異步Redis客戶端實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了php操作Swoole異步Redis客戶端相關(guān)擴(kuò)展安裝與功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-10-10
PHP_NETWORK_GETADDRESSES: GETADDRINFO FAILED問題解決辦法
這篇文章主要介紹了PHP_NETWORK_GETADDRESSES: GETADDRINFO FAILED問題解決辦法,需要的朋友可以參考下2014-05-05
php實(shí)現(xiàn)微信公眾號(hào)無限群發(fā)
本文給大家分享的是php實(shí)現(xiàn)的利用微信的客服接口進(jìn)行各類消息的無限群發(fā),思路非常巧妙,有需要的小伙伴可以參考下2015-10-10

