PHP之thinkphp的数据库操作代码段汇总

来源:互联网 发布:阿莫达非尼 淘宝 编辑:程序博客网 时间:2024/06/10 09:25

## 整理ThinkPHP常见数据库操作API

分类

操作

1

import("lib.Page");
$pg = new \Page(20,2);

引用第三方库

2

public function _initialize(){
     header("Content-Type:text/html;charset=utf8");
}

设置文档的编码方式,支持中文显示和插入中文

3

$img_path = __DIR__.'\..\..\..\Public\Uploads\task_1';
$handler = opendir($img_path);
while( ($filename = readdir($handler)) != "" ) {}
closedir($handler);

表示当前路径 -> 打开指定的文件夹 -> 读取文件夹中文件名 -> 关闭句柄

4

$download_file = array();
array("gif", "png", "jpg","jpeg")
sizeof($users)

数组定义 -> 数组初始化 -> 数组的长度

5

$users = $user_tb->where(array("name" => $_GET["name"]))->select();
order('id asc') , order('id desc')
limit(1) ,limit(10,25)
field('id,name')

条件filter数据记录 -> 记录递增/递减 -> 限定条目输出 -> 输出记录字段

6

$arr["ret"] = false;
$arr["error"] = "user_name not exist.";

构建hash数组

7

cookie("username",$_GET["name"]);
cookie("username",null);
cookie("username");

加元素到Cookie中 -> 清除Cookie中元素 -> 获取Cookie中元素

8

$this->ajaxReturn ($arr,"JSON");

采用json的格式返回ajax请求

9

$this->redirect("Login");

页面重定向

10

$table = M('bug_table');
$table->create();
$table->title = $_GET['title'];
$table->reporter = cookie("username");
$table->add();

添加数据库记录

11

$this->assign('list',$list);
$this->display();

12

date('Y-m-d H:i:s',time());

获取日期

13

file_exists(__DIR__.'\persons.xml')
$person=simplexml_load_file(__DIR__.'\persons.xml');

文件存在性判断 -> 读取xml文件到记录对象

14

foreach($love as $lv){  $lv  }
foreach ($title as $k => $v) { $k为下标,$v为值 }

遍历数组

15

__PUBLIC__ ,__URL__ , __SELF__ ,__DIR__ , ,

 

16

iconv("UTF-8", "GB2312",$v);
implode(" ",$arr);

将变量$v从UTF-8转为GB2312格式
将数组转换为一个字符串

17

$dat['name'] = $_GET["name"];
$dat['passwd'] = $_GET["passwd"];
$table->where('id=%s',1)->save($dat);

更新数据库记录

18

$table->where("id > %d",$id)->delete();

删除数据库记录

 

页面尾部输出用于调试

 


0 0
原创粉丝点击