SELECT `查詢的欄位` [FROM `資料表名稱` 附加的篩選條件]
[where 篩選條件]
[group by `欄位名稱`][having group的篩選條件]
[order by {unsigned_integer | `欄位名稱` | formula} [asc | desc] ,...]
[limit [起點,] 筆數]
$result
$result
的各種取得資料方法,將資料一筆一筆取回。while(){}
迴圈中取回。$result->fetch_assoc()
取出的資料陣列,會以資料表欄位名稱為陣列索引; 以$result->fetch_row()
取出的資料陣列,是以欄位順序為陣列索引,通常搭配list()
使用$data=$result->fetch_assoc();
得到的結果為:
array(2) {
[0]=>
array(9) {
["sn"]=>
string(1) "1"
["title"]=>
string(15) "繳信用卡費"
["directions"]=>
string(21) "全家繳信用卡費"
["end"]=>
string(10) "2020-06-28"
["priority"]=>
string(3) "中"
["assign"]=>
string(3) "我"
["done"]=>
string(1) "0"
["create_time"]=>
string(19) "2020-06-18 15:38:48"
["update_time"]=>
string(19) "2020-06-18 15:38:48"
}
}
//即
$content[0]['sn']=1;
$content[0]['title'] = "繳信用卡費";
$content[0]['directions'] = "全家繳信用卡費";
$content[0]['end'] = "2020-06-28";
$content[0]['priority'] = "中";
$content[0]['assign'] = "我";
$content[0]['done'] = "0";
$content[0]['create_time'] = "2020-06-18 15:38:48";
$content[0]['update_time'] = "2020-06-18 15:38:48";
$data=$result->fetch_row();
得到的結果為:
array(2) {
[0]=>
array(9) {
[0]=>
string(1) "1"
[1]=>
string(15) "繳信用卡費"
[2]=>
string(21) "全家繳信用卡費"
[3]=>
string(10) "2020-06-28"
[4]=>
string(3) "中"
[5]=>
string(3) "我"
[6]=>
string(1) "0"
[7]=>
string(19) "2020-06-18 15:38:48"
[8]=>
string(19) "2020-06-18 15:38:48"
}
}
//即
$content[0][0]=1;
$content[0][1] = "繳信用卡費";
$content[0][2] = "全家繳信用卡費";
$content[0][3] = "2020-06-28";
$content[0][4] = "中";
$content[0][5] = "我";
$content[0][6] = "0";
$content[0][7] = "2020-06-18 15:38:48";
$content[0][8] = "2020-06-18 15:38:48";
list($sn, $title, $directions,$end,$priority,$assign,$done, $create_time, $update_time)=$result->fetch_row();
效果同 $data=$result->fetch_assoc();
// 列出所有
function list_all()
{
global $db, $smarty, $content;
$sql = "select * from `list` order by end";
if (!$result = $db->query($sql)) {
die(error($db->error));
}
$content = [];
// 法一
while ($data = $result->fetch_assoc()) {
$content[] = $data;
}
// 法二(樣板要改,不建議)
while ($data = $result->fetch_row()) {
$content[] = $data;
}
// 法三
$i = 0;
while (list($sn, $title, $directions, $end, $priority, $assign, $done, $create_time, $update_time) = $result->fetch_row()) {
$content[$i]['sn'] = $sn;
$content[$i]['title'] = $title;
$content[$i]['directions'] = $directions;
$content[$i]['end'] = $end;
$content[$i]['priority'] = $priority;
$content[$i]['assign'] = $assign;
$content[$i]['done'] = $done;
$content[$i]['create_time'] = $create_time;
$content[$i]['update_time'] = $update_time;
$i++;
}
}