教会你如何使用tp3连接数据库和一些对数据库的增删改查

连接数据库:在模块的Conf里面的config 配置文件中进行数据库链接
<?php
return array(
//'配置项'=>'配置值'
    'DB_TYPE'               =>  'mysql',     // 数据库类型
    'DB_HOST'               =>  '127.0.0.1', // 服务器地址
    'DB_NAME'               =>  'test',          // 数据库名
    'DB_USER'               =>  'root',      // 用户名
    'DB_PWD'                =>  '',          // 密码
    'DB_PORT'               =>  '3306',        // 端口
    'DB_PREFIX'             =>  '',    // 数据库表前缀 
    'DB_DEBUG'  =>  TRUE, // 数据库调
     'TMPL_TEMPLATE_SUFFIX'  =>  '.tpl',     // 默认模板文件后缀


);


下面是进行数据库的增删改查:创建(Create) 、 更新(Update) 、 读取(Read) 和 删除(Delete)
添加数据:
public function insert(){
$Form = M('Form');     //进行实例化
if($Form->create()) {
$result = $Form->add();   //add  方法为tp3自带的方法  可以进行添加
if($result) {
$this->success('数据添加成功!');
}else{
$this->error('数据添加错误!');
}
}else{
$this->error($Form->getError());
}
}


查询数据:$data = $Form->find($id); find为tp3中自带的的查询, 这是根据id号进行查询


修改数据:$result = $Form->save();  save为自带的自己更新当前内容的方法;


删除数据:  可以调用tp3中自带的delect() 方法;

猜你喜欢

转载自blog.csdn.net/sunshine_penple/article/details/79680756