在对dedecms做二次开发的时候,熟悉dedecms的数据库操作类非常重要,这样可以对dedecms后台进行很多操作开发,在网上找了这篇文章,dedecms的数据库操作类,非常实用,贴出来以备今后自己查阅,也希望能帮到大家。
引入common.inc.php文件:
require_once (dirname(__FILE__) . “/include/common.inc.php”);
获取一条记录的内容:
$row = $dsql->GetOne(“SELECT * FROM `#@__archives` WHERE aid=$aid”);
echo $row[‘id’];
将查询获取总数输出:
$row = $dsql->GetOne(“SELECT count(*) AS count FROM `#@__archives` WHERE typeid = $typeid”);
echo $row[‘count’]; //输出总数
将查询的若干条记录输出:
$sql = “SELECT * FROM `#@__archives` WHERE 1=1”;
$dsql->SetQuery($sql); //将SQL查询语句格式化
$dsql->Execute(); //执行SQL操作
//通过循环输出执行查询中的结果
while($row = $dsql->GetArray()){
echo $row[‘id’];
echo $row[‘title’];
}
//或者采取这种方式输出内容
while($row = $dsql->GetObject()){
echo $row->id;
echo $row->title;
}
插入一条记录:
$sql = “INSERT INTO `#@__arcatt` (att,attname)VALUES(‘$att’,’$attname’);”; //插入记录数据库
$dsql->ExecuteNoneQuery($sql); //执行SQL操作
$gid = $dsql->GetLastID(); //获取刚刚插入的id
删除一条记录:
$sql = “Delete From `#@__arcatt` where sortid=’$sortid'”;
$dsql->SetQuery($sql);
$dsql->ExecNoneQuery();
//或者使用简化模式
$dsql->ExecNoneQuery(“Delete From `#@__arcatt` where sortid=’$sortid'”);
更新一条记录:
$sql = “Update `#@__archives` set title=’$title’,shorttitle=’$shorttitle’ where aid=’$aid'”;
$rs = $dsql->ExecuteNoneQuery($sql);
判断获取数据库内容的常用方法:
//查询
$row = $dsql->GetOne(“Select * From `#@__archives` where aid=$aid”);
if(!is_array($row)){
echo “失败”;
exit();
}
//新增、修改、删除(返回操作条数)
$upquery = ” Update dede_member_flink set title=’$title’,shorttitle=’$shorttitle’ where aid=’$aid'”;
$rs = $dsql->ExecuteNoneQuery($upquery);
if($rs){
echo “成功”;
}else{
echo “失败”;
}
获取总数:
$dsql = new DedeSql(false);
$dsql->SetQuery(“SELECT * FROM `#@__archives` WHERE 1=1”);
$dsql->Execute();
$ns = $dsql->GetTotalRow();
关闭数据库:
$dsql->Close();