您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

一个可以把配置参数保存到数据库的函数

2025/10/3 20:20:46发布11次查看
不知道tp有没有这个功能,自己做了一个可以把配置参数保存到数据库,thinkphp 3.2放到
/项目目录/common/common/function.php里就可以使用了。
function cd($key, $value = null){
/**
    *    公共方法,可以把配置参数保存到数据库
    *
    *    用法:
    *    cd('配置项');            //读取配置项
    *    cd('配置项', '数据')    //写入配置项
    *    cd('配置项', 'del')    //从数据库中移除该配置项
    *
    *
    *在数据库中建立config表,表前缀改成自己的
    * create table if not exists `think_config` (
    *   `id` int(11) not null auto_increment,
    *   `key` varchar(255) character set utf8 not null,
    *   `value` varchar(255) character set utf8 not null,
    *   primary key (`id`)
    * ) engine=myisam default charset=utf8 auto_increment=1 ;
    */
$cd = m('config');
    $where = array( 'key' => $key);
//如果有$value为空,就返回$key的值
    if($value == null){
        return $cd->where( $where )->getfield('value');
    }
    //如果$value为del,就删除这条记录的值
    else if($value == del)
    {
        return $cd->where( $where )->delete();
    }
    //如果$value有正常值,其它操作
    else{
//如果存在$key,更新
        if($cd->where($where)->find()){
            $cd->value = $value;
            return $cd->where($where)->save();
        }
        //如果不存在$key,写入一条新数据
        else{
            $cd->key = $key;
            $cd->value = $value;
            return $cd->add();
        }
    }
}
ad:真正免费,域名+虚机+企业邮箱=0元
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product