mysqli操做數據庫

1 鏈接數據庫:可使用對象或函數來鏈接(咱們這裏主要用mysqli對象,附帶着函數鏈接)php

//建立mysqli對象(也能夠叫作資源句柄)
$_mysqli = new mysqli();
//鏈接數據庫
//若是不使用面向對象,徹底可使用mysqli_connect()函數來鏈接
$_mysqli->connect('localhost', 'root', 'kang123456', 'guest');

//斷開mysqli
//mysqli_close()函數
$_mysqli->close();

還能夠建立對象時直接鏈接mysql

//建立mysqli對象時直接鏈接數據庫
$_mysqli = new mysqli('localhost', 'root', 'kang123456', 'guest');
//另外選擇一個數據庫
$_mysqli->select_db('testguest');

$_mysqli->close();

2 錯誤:鏈接錯誤和操做錯誤sql

鏈接錯誤:數據庫

//鏈接錯誤
//當由於參數錯誤致使鏈接失敗是,對象沒有建立成功,因此就沒有權力調用mysqli對象的方法,因此要用函數方式去捕捉錯誤
@$_mysqli = new mysqli('localhost', 'roo', 'kang123456', 'guest');
//0表示沒有任何錯誤
if (mysqli_connect_errno()){
    echo '數據庫鏈接錯誤'.mysqli_connect_error();
    exit();
}
$_mysqli->close();

操做錯誤:數組

//操做錯誤
@$_mysqli = new mysqli('localhost', 'root', 'kang123456', 'guest');
//選擇一個不存在的數據庫,產生操做錯誤
$_mysqli->select_db('dfas');
//操做錯誤
if ($_mysqli->errno){
    echo '數據庫操做錯誤'.$_mysqli->error;
    exit();
}
$_mysqli->close();

3 與數據庫進行交互:建立 獲取 更新 刪除函數

建立與獲取oop

<?php
//操做錯誤
    @$_mysqli = new mysqli('localhost', 'root', 'kang123456', 'testguest');
    //1 設置編碼
    $_mysqli->set_charset('utf8');
    //2 建立sql
    $_sql = 'select * from tg_user';
    //3 執行sql,把結果集賦給變量
    $_result = $_mysqli->query($_sql);
    //取得第一行數據,運行一次,指針下移一條
    $_user = $_result->fetch_row();
    //4 獲取
    //4-1 使用索引數組循環出用戶名
    while (!!$_row = $_result->fetch_row()){
        echo $_row[3].'<br />';
    }
    //4-2 使用關聯數組循環出用戶名
    while (!!$_assoc = $_result->fetch_assoc()){
        echo $_assoc['tg_username'].'<br />';
    }
    //4-3 使用索引加關聯數組
    print_r($_result->fetch_array());
    //4-4 使用oop方式,但返回的是對象
    while (!!$_object = $_result->fetch_object()){
        echo $_object->tg_username.'<br />';
    }
?>

查看選擇了多少行和影響了多少行:影響了多少行是mysqli下的屬性fetch

    //肯定選擇了多少行和受影響的行
    @$_mysqli = new mysqli('localhost', 'root', 'kang123456', 'testguest');
    if ($_mysqli->errno){
        echo '數據庫鏈接錯誤'.$_mysqli->error;
    }
    $_mysqli->set_charset('utf8');
    $_sql = 'select * from tg_user limit 0,10';
    $_result = $_mysqli->query($_sql);
    //看選擇了多少行,只有查
    echo $_result->num_rows; //10
    //增刪改查影響了多少行(若是改爲同樣的影響0行)
    echo $_mysqli->affected_rows;//10

獲取字段(列)編碼

    //1 查看結果集裏有多少字段(列)
    echo $_result->field_count;
    //2 獲取字段的名字,一次一個,指針下移
    $_field = $_result->fetch_field();//返回的是對象
    echo $_field->name; //tg_id
    print_r($_field);//打印以下
/*     stdClass Object
    (
        [name] => tg_id
        [orgname] => tg_id
        [table] => tg_user
        [orgtable] => tg_user
        [def] =>
        [db] => testguest
        [catalog] => def
        [max_length] => 2
        [length] => 8
        [charsetnr] => 63
        [flags] => 49699
        [type] => 9
        [decimals] => 0
    ) */
    //遍歷
    while (!!$_field = $_result->fetch_field()) {
        echo $_field->name.'<br />';
    }
    //3 一次性取得全部字段
    $_fields = $_result->fetch_fields(); //返回的是數組,每一個是和上邊同樣的對象
    print_r($_fields);
    //遍歷
    foreach ($_fields as $_field){
        echo $_field->name.'<br />';
    }

移動指針:移動數據指針和移動字段指針spa

    //移動指針
    //1 移動數據指針
    $_result->data_seek(0);//移動到第0個就是原來的位置
    $_row = $_result->fetch_row();
    echo $_row[3];
    
    //2 移動字段指針
    $_result->field_seek(0);
    $_field = $_result->fetch_field();
    echo $_field->name; //tg_id

多條sql語句一塊兒執行

    //建立三個修改的sql語句
    $_sql .= 'update tg_user set tg_username="黨興明" where tg_id=43;';
    $_sql .= 'update tg_flower set tg_fromuser="黨興明" where tg_id=1;';
    $_sql .= 'update tg_friend set tg_fromuser="黨興明" where tg_id=1';
    //一塊兒執行
    $_mysqli->multi_query($_sql);
    //建立三條選擇數據
    $_sql .= 'select * from tg_user;';
    $_sql .= 'select * from tg_flower;';
    $_sql .= 'select * from tg_friend';
    //echo $_mysqli->multi_query($_sql);  //1
    if ($_mysqli->multi_query($_sql)){
        //獲取當前結果集
        $_result = $_mysqli->store_result();  //第一條sql語句
        print_r($_result->fetch_row());
        echo '<br >';
        //將結果集指針移動到下一條
        $_mysqli->next_result();
        $_result = $_mysqli->store_result();  //第二條sql語句
        print_r($_result->fetch_row());
        echo '<br >';
        //將結果集指針移動到下一條
        $_mysqli->next_result();
        $_result = $_mysqli->store_result();  //第三條sql語句
        print_r($_result->fetch_row());
    }else {
        echo '錯誤';
        exit();
    }

事務:

    //事務
    @$_mysqli = new mysqli('localhost', 'root', 'kang123456', 'testguest');
    $_mysqli->set_charset('utf8');
    //關閉自動提交
    $_mysqli->autocommit(false);
    //建立兩個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';
    
    //執行多條sql語句,只有兩句都成功就成功,不然回滾
    if ($_mysqli->multi_query($_sql)){
        //經過影響的行數,來判斷是否成功執行,若是sql語句有誤,那麼就執行回滾,不然手工提交
        $_success = $_mysqli->affected_rows == 1 ? true : false;
        //下移指針
        $_mysqli->next_result();
        $_success2 = $_mysqli->affected_rows == 1 ? true : false;
        //若是兩條都成功
        if ($_success && $_success2){
            //手工提交
            $_mysqli->commit();
            echo '完美提交';
        }else {
            //不然回滾
            $_mysqli->rollback();
            echo '回滾, 操做歸零';
        }
    }else {
        echo '第一條有錯誤';
        exit();
    }
    //開啓自動提交
    $_mysqli->autocommit(true);
相關文章
相關標籤/搜索