1、执行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、计算总数
$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;
系统的表前缀可以使用变量$DT_PRE(一般在语句中使用)或者常量DT_PRE(一般在函数中使用)。
如果在函数中使用数据库操作,需要先进行global $db;
自V7.0,可以在系统任意位置使用DB::代替$db->进行数据库操作,尤其是函数中,无需考虑是否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);