1、執(zhí)行SQL語句
$db->query("INSERT INTO `{$DT_PRE}table` (`xxx`) VALUES ('yyy')");
$db->query("UPDATE `{$DT_PRE}table` SET `xxx`='yyy' WHERE `zzz`=1");
$db->query("DELETE FROM `{$DT_PRE}table` WHERE `zzz`=1");
2、讀取單條信息
$r = $db->get_one("SELECT * FROM `{$DT_PRE}table` WHERE `xxx`='yyy'");
print_r($r);
print_r($r);
3、計算總數(shù)
$r = $db->get_one("SELECT COUNT(*) AS num FROM `{$DT_PRE}table` WHERE `xxx`='yyy'");
echo $r['num'];
echo $r['num'];
4、讀取多條信息
$lists = array();
$result = $db->query("SELECT * FROM `{$DT_PRE}table` WHERE `xxx`='yyy' ORDER BY `zzz` DESC LIMIT 0,10");
while($r = $db->fetch_array($result)) {
$lists[] = $r;
}
print_r($lists);
$result = $db->query("SELECT * FROM `{$DT_PRE}table` WHERE `xxx`='yyy' ORDER BY `zzz` DESC LIMIT 0,10");
while($r = $db->fetch_array($result)) {
$lists[] = $r;
}
print_r($lists);
5、分頁
$lists = array();
$r = $db->get_one("SELECT COUNT(*) AS num FROM `{$DT_PRE}table` WHERE `xxx`='yyy'");
$pages = pages($r['num'], $page, $pagesize);
$result = $db->query("SELECT * FROM `{$DT_PRE}table` WHERE `xxx`='yyy' ORDER BY `zzz` DESC LIMIT $offset,$pagesize");
while($r = $db->fetch_array($result)) {
$lists[] = $r;
}
print_r($lists); echo $pages;
$r = $db->get_one("SELECT COUNT(*) AS num FROM `{$DT_PRE}table` WHERE `xxx`='yyy'");
$pages = pages($r['num'], $page, $pagesize);
$result = $db->query("SELECT * FROM `{$DT_PRE}table` WHERE `xxx`='yyy' ORDER BY `zzz` DESC LIMIT $offset,$pagesize");
while($r = $db->fetch_array($result)) {
$lists[] = $r;
}
print_r($lists); echo $pages;
系統(tǒng)的表前綴可以使用變量$DT_PRE(一般在語句中使用)或者常量DT_PRE(一般在函數(shù)中使用)。
如果在函數(shù)中使用數(shù)據(jù)庫操作,需要先進行global $db;
自V7.0,可以在系統(tǒng)任意位置使用DB::代替$db->進行數(shù)據(jù)庫操作,尤其是函數(shù)中,無需考慮是否global $db;
例如:
$lists = array();
$result = $db->query("SELECT * FROM `{$DT_PRE}table` WHERE `xxx`='yyy' ORDER BY `zzz` DESC LIMIT 0,10");
while($r = $db->fetch_array($result)) {
$lists[] = $r;
}
print_r($lists);
可以改為:$result = $db->query("SELECT * FROM `{$DT_PRE}table` WHERE `xxx`='yyy' ORDER BY `zzz` DESC LIMIT 0,10");
while($r = $db->fetch_array($result)) {
$lists[] = $r;
}
print_r($lists);
$lists = array();
$result = DB::query("SELECT * FROM `{$DT_PRE}table` WHERE `xxx`='yyy' ORDER BY `zzz` DESC LIMIT 0,10");
while($r = DB::fetch_array($result)) {
$lists[] = $r;
}
print_r($lists);
$result = DB::query("SELECT * FROM `{$DT_PRE}table` WHERE `xxx`='yyy' ORDER BY `zzz` DESC LIMIT 0,10");
while($r = DB::fetch_array($result)) {
$lists[] = $r;
}
print_r($lists);