PHP如何使用Mysqli操作數(shù)據(jù)庫-創(chuàng)新互聯(lián)

這篇文章給大家分享的是有關(guān)PHP如何使用Mysqli操作數(shù)據(jù)庫的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

十多年的長寧網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。成都營銷網(wǎng)站建設(shè)的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整長寧建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)公司從事“長寧網(wǎng)站設(shè)計”,“長寧網(wǎng)站推廣”以來,每個客戶項目都認(rèn)真落實執(zhí)行。

具體如下:

Demo1.php

<?php
  //使用 mysqli 對象操作數(shù)據(jù)庫
  //創(chuàng)建 mysqli 對象(資源句柄)
  $_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ú)選擇一個數(shù)據(jù)庫
  //這里選擇的數(shù)據(jù)庫會替代上面的數(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)錯誤,導(dǎo)致連接錯誤的時候,
  //$_mysqli 這個對象就沒有創(chuàng)建成功,也就是說,沒有資源句柄的功能
  //就是沒有調(diào)用 mysqli 下的方法和屬性的能力了
  @$_mysqli = new mysqli('localhost','root','123456','guest');
  //為什么要用函數(shù)去捕捉呢?
  //為什么不用面向?qū)ο蟮姆绞饺ゲ蹲侥兀?  if(mysqli_connect_errno()){
    echo '數(shù)據(jù)庫連接出現(xiàn)了錯誤,錯誤的信息是:'.mysqli_connect_error();
    exit();
  }
  $_mysqli->close();
?>

Demo4.php

<?php
  header ( 'Content-Type:text/html; charset=utf-8;' );
  //連接 mysql
  //當(dāng)你參數(shù)出現(xiàn)錯誤,導(dǎo)致連接錯誤的時候,
  //$_mysqli 這個對象就沒有創(chuàng)建成功,也就是說,沒有資源句柄的功能
  //就是沒有調(diào)用 mysqli 下的方法和屬性的能力了
  @$_mysqli = new mysqli('localhost','root','123456','guest');
  //為什么要用函數(shù)去捕捉呢?
  //為什么不用面向?qū)ο蟮姆绞饺ゲ蹲侥兀?  if(mysqli_connect_errno()){
    echo '數(shù)據(jù)庫連接出現(xiàn)了錯誤,錯誤的信息是:'.mysqli_connect_error();
    exit();
  }
  //$_mysqli -> select_db('fsdfd');
  //數(shù)據(jù)庫操作時發(fā)生的錯誤
  if($_mysqli -> errno){
    echo '數(shù)據(jù)庫操作錯誤:'.$_mysqli -> error;
  }
  $_mysqli->close();
?>

Demo5.php

<?php
  header ( 'Content-Type:text/html; charset=utf-8;' );
  $_mysqli = new mysqli('localhost','root','123456','testguest');
  //數(shù)據(jù)庫連接時發(fā)生的錯誤
  if(mysqli_connect_errno()){
    echo '數(shù)據(jù)庫連接出現(xiàn)了錯誤,錯誤的信息是:'.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();是返回的一個數(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ù)庫連接時發(fā)生的錯誤
  if (mysqli_connect_errno()) {
    echo '數(shù)據(jù)庫連接出現(xiàn)了錯誤.錯誤的信息是:'.mysqli_connect_error();
    exit();
  }
  //設(shè)置一下編碼
  $_mysqli->set_charset('utf8');
  //創(chuàng)建一句SQL,獲取數(shù)據(jù)庫的表的數(shù)據(jù)
  $_sql = "SELECT * FROM tg_user";
  //創(chuàng)建一個結(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ù)庫連接時發(fā)生的錯誤
  if (mysqli_connect_errno()) {
    echo '數(shù)據(jù)庫連接出現(xiàn)了錯誤.錯誤的信息是:'.mysqli_connect_error();
    exit();
  }
  //設(shè)置一下編碼
  $_mysqli->set_charset('utf8');
  //創(chuàng)建一句SQL,獲取數(shù)據(jù)庫的表的數(shù)據(jù)
  $_sql = "SELECT * FROM tg_user";
  //創(chuàng)建一個結(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ù)庫連接時發(fā)生的錯誤
  if (mysqli_connect_errno()) {
    echo '數(shù)據(jù)庫連接出現(xiàn)了錯誤.錯誤的信息是:'.mysqli_connect_error();
    exit();
  }
  //設(shè)置一下編碼
  $_mysqli->set_charset('utf8');
  //創(chuàng)建一句SQL,獲取數(shù)據(jù)庫的表的數(shù)據(jù)
  $_sql = "SELECT * FROM tg_user";
  //創(chuàng)建一個結(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ù)庫連接時發(fā)生的錯誤
  if (mysqli_connect_errno()) {
    echo '數(shù)據(jù)庫連接出現(xiàn)了錯誤.錯誤的信息是:'.mysqli_connect_error();
    exit();
  }
  //設(shè)置一下編碼
  $_mysqli->set_charset('utf8');
  //創(chuàng)建一句SQL,獲取數(shù)據(jù)庫的表的數(shù)據(jù)
  $_sql = "SELECT * FROM tg_user";
  //創(chuàng)建一個結(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ù)庫連接時發(fā)生的錯誤
  if (mysqli_connect_errno()) {
    echo '數(shù)據(jù)庫連接出現(xiàn)了錯誤.錯誤的信息是:'.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)建一個結(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ù)庫連接時發(fā)生的錯誤
  if (mysqli_connect_errno()) {
    echo '數(shù)據(jù)庫連接出現(xiàn)了錯誤.錯誤的信息是:'.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)建一個結(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ù)庫連接時發(fā)生的錯誤
  if (mysqli_connect_errno()) {
    echo '數(shù)據(jù)庫連接出現(xiàn)了錯誤.錯誤的信息是:'.mysqli_connect_error();
    exit();
  }
  //設(shè)置一下編碼
  $_mysqli->set_charset('utf8');
  //創(chuàng)建一句SQL,獲取數(shù)據(jù)庫的表的數(shù)據(jù)
  $_sql = "SELECT * FROM tg_user";
  //創(chuàng)建一個結(jié)果集
  $_result = $_mysqli->query($_sql);
  //求出表中有多少個字段
  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ù)庫連接時發(fā)生的錯誤
  if (mysqli_connect_errno()) {
    echo '數(shù)據(jù)庫連接出現(xiàn)了錯誤.錯誤的信息是:'.mysqli_connect_error();
    exit();
  }
  //設(shè)置一下編碼
  $_mysqli->set_charset('utf8');
  //創(chuàng)建一句SQL,獲取數(shù)據(jù)庫的表的數(shù)據(jù)
  $_sql = "SELECT * FROM tg_user";
  //創(chuàng)建一個結(jié)果集
  $_result = $_mysqli->query($_sql);
  //移動數(shù)據(jù)指針
  $_result->data_seek(9);
  $_row = $_result->fetch_row();
  echo $_row[3];
  //移動字段指針
  $_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ù)庫連接時發(fā)生的錯誤
  if (mysqli_connect_errno()) {
    echo '數(shù)據(jù)庫連接出現(xiàn)了錯誤.錯誤的信息是:'.mysqli_connect_error();
    exit();
  }
  //設(shè)置一下編碼
  $_mysqli->set_charset('utf8');
  //創(chuàng)建三個修改的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ù)庫連接時發(fā)生的錯誤
  if (mysqli_connect_errno()) {
    echo '數(shù)據(jù)庫連接出現(xiàn)了錯誤.錯誤的信息是:'.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ù)庫連接時發(fā)生的錯誤
  if (mysqli_connect_errno()) {
    echo '數(shù)據(jù)庫連接出現(xiàn)了錯誤.錯誤的信息是:'.mysqli_connect_error();
    exit();
  }
  //設(shè)置一下編碼
  $_mysqli->set_charset('utf8');
  //設(shè)置關(guān)閉自動提交(手工提交)
  $_mysqli->autocommit(false);
  //創(chuàng)建兩個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語句有錯誤!';
  }
  //再開啟自動提交
  $_mysqli->autocommit(true);
  $_mysqli->close();
?>

感謝各位的閱讀!關(guān)于“PHP如何使用Mysqli操作數(shù)據(jù)庫”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

當(dāng)前文章:PHP如何使用Mysqli操作數(shù)據(jù)庫-創(chuàng)新互聯(lián)
標(biāo)題URL:http://muchs.cn/article8/dsgcip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化企業(yè)網(wǎng)站制作、建站公司、云服務(wù)器、軟件開發(fā)App設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站