PHP MySQL (一)程式導向 增刪查改

一句話兒發表於2020-01-31

使用php操作mysql資料庫,需安裝mysqli擴充

作用:用於操作mysql資料庫,增刪查改

檢測是否開啟了擴充
<?php
    //1.驗證mysqli擴充套件是否已經開啟
    phpinfo();
    //2.檢測擴充套件是否已經載入
    var_dump(extension_loaded('mysqli'));
    //開啟返回true
    //3.檢測函式是否存在
    var_dump(function_exsts('mydqli_connect'));
    //4.得到開啟的擴充套件
    print_r(get_loaded_extensions());
sql原生語句增刪改查

INSERT INTO table_name ( field1, field2,...fieldN )
VALUES
( value1, value2,...valueN );

示例:
INSERT INTO user (username, password, create_time, update_time)
VALUES
('seven', 'seven', '2020-01-14 23:59:24', '2020-01-14 23:59:24')

DELETE FROM table_name WHERE condition

示例:

DELETE FROM user WHERE id = 1

SELECT filed FROM table_name WHERE condition

示例:

SELECT * FROM user WHERE username = 'seven'

UPDATE table_name SET field1=new-value1, field2=new-value2

[WHERE Clause]

示例:

UPDATE user SET password = 'seven' WHERE username = 'seven'

header('content-type:text/html;charset=utf-8');

$host = '127.0.0.1';
$user = 'root';
$password = 'root';
$db = 'test';

$link = mysqli_connect($host, $user, $password,$db) or die('connect error'.mysqli_connect_errno().":".mysqli_connect_error());
mysqli_set_charset($link,'utf8');//設定字符集,字符集區別

$username = 'seven';
$password = md5("seven");
$create_time = date('Y-m-d H:i:s');
$update_time = date('Y-m-d H:i:s');
//增加
$sql = "insert user(username, password, create_time, update_time) values('{$username}', '{$password}', '{$create_time}', '{$update_time}')";
$res = mysqli_query($link, $sql); //返回true or false
if($res){
    echo  '自增長id'.mysqli_insert_id($link);
    echo PHP_EOL;
    echo  '受影響的行數'.mysqli_affected_rows($link);
}else{
    echo 'error'.PHP_EOL.mysqli_errno($link).":".mysqli_error($link);
}
<?php
    header('content-type:text/html;charset=utf-8');

    $host = '127.0.0.1';
    $user = 'root';
    $password = 'root';
    $db = 'test';

    $link = mysqli_connect($host, $user, $password,$db) or die('connect error'.mysqli_connect_errno().":".mysqli_connect_error());
    mysqli_set_charset($link,'utf8');//設定字符集,字符集區別

    $sql = 'delete from user where id = 1';
    $result = mysqli_query($sql);
    if($result && myslqi_num_rows($result)>0){
        while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
            $rows[]=$row;
        }
    }
    //釋放結果集
    mysqli_free_result($result);
    //關閉連線
    mysqli_close($link);
<?php
    header('content-type:text/html;charset=utf-8');

    $host = '127.0.0.1';
    $user = 'root';
    $password = 'root';
    $db = 'test';

    $link = mysqli_connect($host, $user, $password,$db) or die('connect error'.mysqli_connect_errno().":".mysqli_connect_error());
    mysqli_set_charset($link,'utf8');//設定字符集,字符集區別

    $sql = 'select * from user';
    $result = mysqli_query($sql);
    if($result && myslqi_num_rows($result)>0){
        while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
            $rows[]=$row;
        }
    }
    //釋放結果集
    mysqli_free_result($result);
    //關閉連線
    mysqli_close($link);
<?php
    header('content-type:text/html;charset=utf-8');
    $host = '127.0.0.1';
    $user = 'root';
    $password = 'root';
    $db = 'test';

    $link = mysqli_connect($host, $user, $password,$db) or die('connect error'.mysqli_connect_errno().":".mysqli_connect_error());
    mysqli_set_charset($link,'utf8');//設定字符集,字符集區別

    // 修改
    $sql = 'update user set age=age+10 where id=41';
    $res = mysqli_query($link,$sql); //執行多條sql語句
    if($res){
        echo  '自增長id'.mysqli_insert_id($link);
        echo PHP_EOL;
        echo  '受影響的行數'.mysqli_affected_rows($link);
    }else{
        echo 'error:<br/>';
        echo mysqli_errno($link),":".$mysqli_error($link);
    }
    //關閉連線
    mysqli_close($link);
本作品採用《CC 協議》,轉載必須註明作者和本文連結
寫的不好,就當是整理下思緒吧。

相關文章