-
Notifications
You must be signed in to change notification settings - Fork 3
/
demo.php
104 lines (75 loc) · 2.95 KB
/
demo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
require_once "config.php";
require_once "PDO_MySQL.class.php";
/* 以下仅为使用示例 */
// 配置文件初始化
$link = M("fills");
// 数组参数初始化
$dbConfig = array(
'hostname' => '127.0.0.1',
'username'=>'root',
'password'=>'root',
'database'=>'db_name',
);
$link = M('fills', 1, $dbConfig);
# 如果需要此配置(上面的$dbConfig数组)切换数据表,既可以使用$link->table('other_table'),也可以如下使用
$other_table = M('other_table', 1);
## 1. select
//SELECT * FROM `fills` WHERE ( ( `degree` = '-1' AND `teacher` = 'who' ) OR `id` > '5' OR `status` <> '0' ) AND `subject` = '2' AND `chapter` >= '7'
$subsubmap['degree']=-1;
$subsubmap['teacher']='who';
$submap['_complex']=$subsubmap;
$submap['id']=array('gt',5);
$submap['status']=array('neq',0);
$submap['_logic']='or';
$map['_complex']=$submap;
$map['subject']=2;
$map['chapter']=array('egt',7);
dump($link->where($map)->select());
//SELECT fills.id,users.id AS `id_users`,concat(name,'-',id) AS `truename`,LEFT(title,7) AS `sub_title` FROM fills,chat.users
$field=array(
'fills.id',
'users.id'=>'id_users',
"concat(name,'-',id)"=>'truename',
'LEFT(title,7)'=>'sub_title'
);
dump($link->table('fills,chat.users')->field($field)->select()); //返回二维数组或null或false
## 2. find
//SELECT * FROM `fills` WHERE `id` = '4' LIMIT 1
dump($link->find(4)); //返回一维数组或null或false
## 3. add
//INSERT INTO users (`user_id`,`nick`,`school`) VALUES ('abcd1234','frankie','scut')
$data['user_id']='abcd1234';
$data['nick']='frankie';
$data['school']='scut';
dump($link->table('users')->add($data)); //返回lastID或false
## 4. save
//UPDATE users SET `nick` = 'frankie123',`school` = 'scut' WHERE `user_id` = 'abcd1234'
$data['user_id']='abcd1234';
$data['nick']='frankie123';
$data['school']='scut';
dump($link->table('users')->save($data)); //返回更新影响数或false
## 5. setField
//UPDATE users SET `nick` = 'test' WHERE `user_id` = 'abcd1234'
$save_where['user_id']='abcd1234';
dump($link->table('users')->where($save_where)->setField('nick','test')); //返回更新影响数或false
## 6. delete
//DELETE FROM `fills` WHERE `id` > '54' AND `subject` = '1'
$delete_where['id']=array('gt',54);
$delete_where['subject']=1;
dump($link->where($delete_where)->delete()); //返回删除成功的行数或false
//DELETE t2 FROM users as t1 INNER JOIN chat.users as t2 on t1.user_id=t2.user_id
dump($link->table('users as t1')->join('chat.users as t2 on t1.user_id=t2.user_id')->delete('t2'));
## 7. 打印当前模型执行的最后一条SQL语句
dump($link->getLastSql());
dump($link->_sql());
## 8. 事务驱动
$link->startTrans(); # 开启事务
$link->inTrans(); # 判断事务处于事务中,是则返回true
$link->where('u_id=1')->delete();
if ( $something == true ) {
$link->commit();
} else {
$link->rollback();
}
?>