search
Homephp教程php手册一个可以把配置参数保存到数据库的函数

不知道TP有没有这个功能,自己做了一个可以把配置参数保存到数据库,Thinkphp 3.2放到
/项目目录/Common/Common/function.php里就可以使用了。
function CD($key, $value = null){<br> <br>     /**<br>     *    公共方法,可以把配置参数保存到数据库<br>     *<br>     *    用法:<br>     *    CD('配置项');            //读取配置项<br>     *    CD('配置项', '数据')    //写入配置项<br>     *    CD('配置项', 'DEL')    //从数据库中移除该配置项<br>     *<br>     *<br>     *在数据库中建立config表,表前缀改成自己的<br>     * CREATE TABLE IF NOT EXISTS `think_config` (<br>     *   `id` int(11) NOT NULL AUTO_INCREMENT,<br>     *   `key` varchar(255) CHARACTER SET utf8 NOT NULL,<br>     *   `value` varchar(255) CHARACTER SET utf8 NOT NULL,<br>     *   PRIMARY KEY (`id`)<br>     * ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;<br>     */<br> <br>     $cd = M('Config');<br>     $where = array( 'key' => $key);<br> <br>     //如果有$value为空,就返回$key的值<br>     if($value == null){<br>         return $cd->where( $where )->getField('value');<br>     }<br>     //如果$value为DEL,就删除这条记录的值<br>     else if($value == "DEL")<br>     {<br>         return $cd->where( $where )->delete();<br>     }<br>     //如果$value有正常值,其它操作<br>     else{<br> <br>         //如果存在$key,更新<br>         if($cd->where($where)->find()){<br>             $cd->value = $value;<br>             return $cd->where($where)->save();<br>         }<br>         //如果不存在$key,写入一条新数据<br>         else{<br>             $cd->key = $key;<br>             $cd->value = $value;<br>             return $cd->add();<br>         }<br>     }<br> }

AD:真正免费,域名+虚机+企业邮箱=0元

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool