1. 程式人生 > >PHP增、刪、改、查及連線資料庫

PHP增、刪、改、查及連線資料庫

增刪改查:

增加:insert into 表名 (欄位1,欄位2,欄位3...) value/values ('值1','值2','值3'....)

刪除:delete from 表名 where 條件

修改:update 表名 set 欄位1='值1',欄位2='值2'... where 條件

查詢:select *(欄位名 as 別名) from 表名 as 別名 【where 欄位1='值1'/欄位1 like '%值1%' and(or) 欄位2='值2'/欄位2 like '%值2%'】【 order by 欄位 asc/desc】【 limit 偏移量,顯示條數】
欄位:* ,具體的欄位名,count(*),sum(欄位名),avg(欄位名)
1、select * from 表名
2、select * from 表名 where 條件
3、select * from 表名 order by 欄位 asc/desc
4、select * from 表名 limit 偏移量,顯示條數
5、select * from 表名 where 條件 limit 偏移量,顯示條數
6、select * from 表名 where 條件 order by 欄位 asc/desc
7、select * from 表名 order by 欄位 asc/desc limit 偏移量,顯示條數
8、select count(*) from 表名
9、select count(*) from 表名 where 條件
10、select count(*) from 表名 order by 欄位 asc/desc
11、select count(*) from 表名 limit 偏移量,顯示條數
12、select count(*) from 表名 where 條件 limit 偏移量,顯示條數
13、select count(*) from 表名 where 條件 order by 欄位 asc/desc
14、select count(*) from 表名 order by 欄位 asc/desc limit 偏移量,顯示條數


專案裡編碼格式
1、php編碼格式 header("content-type:text/html;charset=utf-8");
2、編輯器編碼格式 utf-8
3、資料庫、資料表的編碼格式 utf-8
4、php執行資料庫的sql語句操作,php執行設定資料庫編碼格式
5、前端頁面 utf-8


1、連結資料庫的函式
連線資料庫的資源物件 = mysqli_connect('ip地址','使用者名稱','密碼'[,'資料庫名'])
2、選擇資料庫
mysqli_select_db('連線資料庫的資源物件','資料庫名');
3、設定編碼格式
mysqli_query('連線資料庫的資源物件','set names utf8')
4、編寫sql語句

5、執行sql語句
mysqli_query('連線資料庫的資源物件','編碼SQL語句')

6、查詢sql(取出結果)
mysqli_fetch_assoc(): 一次只能取一條陣列(結果)
mysqli_fetch_array():一次只能取一條陣列(結果)

取多條資料
$data = [];
while($row = mysqli_fetch_assoc($query)){
$data[] = $row1;
}


PHP阻止程式碼執行 exit die
1、echo '連線失敗';die;
2、die('連線失敗');
3、echo '連線失敗';exit;