数据表插入: p4 F, H4 _3 c6 D. A; S8 M
方法: 0 m% S# Z) H8 Z
DB::insert() 1 s. m* y0 a1 Z" ?, s. T
参数:
& q& S$ A. \! x' R. i6 U/ g. F$table:插入数据的表 ) U& R. K c: @. `4 q/ E: I
$data:插入的数据,字段对应值
% J. O5 z+ ]9 t) Q4 j& a$return_insert_id:是否返回插入数据的ID
6 {) a! R5 M. Q3 g2 ?# q6 x$replace:是否使用replace into(当原始表中存在不重复数据就插入,存在就更新) # c2 g$ G) p. I. u( U' d# l) e. E
$silent:操作失败是否不提示
& C A7 P# l+ F' Z
, S; d7 v$ p( M; T* H( U实例
% n' j P# x1 q" yDB::insert('test_db',array(
# C* k2 e7 m, e# t'name' => 'ppc',
& h8 k" c8 a( h- L5 D( ^! g4 O),true,true);
. }- B$ ^+ [+ U- s* \. _
6 h) i ~+ @' x数据表删除:
% f4 S( Y/ I: b# |: ~方法: * f5 H3 B3 p; L, ~: t4 c& }
DB::delete()
# _7 j" J0 [, M2 h7 E# O7 r参数: 4 o4 ^% X% @$ q
$table:删除数据的表
8 R3 K+ |. }. Q% B8 q$condition:删除的条件 - c; @/ p! m" |# N) N0 f" p8 t! L
$limit:删除满足条件的条目数
1 t6 C, P& H8 I9 r3 i$unbufered:是否使用无缓存查询
8 r1 O! t* [6 a) C& q3 o
* b6 K4 l% A! y实例:
) U7 A8 B# R7 EDB::delete('test_db','id>2',1,true);m
6 T9 J5 |; b1 u" `2 V; @/ S9 M; D' r
数据表更新:
) z3 v3 s0 ^7 N. D方法:
) z! l V6 }# J; u7 d; z: \DB::update() % ?' e6 B6 W0 |$ E
参数: 7 `3 d, |. S R7 H {
$table:更新数据的表 3 X6 ~6 C: P; R: d6 }
$data:更新的数据,字段对应值 a; P1 f, |! ]: a9 N
$condition:更新的条件 2 K) m8 `) ^( E) ^9 w
$unbufered:是否使用无缓存查询 : V- n$ ]. i" Q% q) N7 L' j
$low_priority:是否采用无锁表更新
: y+ A' d$ z) W7 P# E8 j5 h# l/ b
4 a' i3 z/ l; s8 h数据表查询(单条):
2 ?6 r5 x$ I; r* w. i" V方法: * l7 G# n# r/ f) R( G- b" ]$ P$ T
DB::fetch_first()
4 d7 h5 r3 H) b5 g参数:
! o' i' w9 j% t* u' f5 X$sql:查询数据的SQL语句 * e! j: y$ Y8 f) I
$arg:绑定查询的参数 . L, C# {1 I" {: v" P
$silent:查询失败是否不提示 ) D, o8 `# M; j, \
实例:
O( ^; d/ @$ m5 c6 z6 K$id = $_GET['id'];
, C& C; f! {6 r8 ?: NDB::fetch_first('select * from %t where id=%d',array( 0 k( C% k* V/ T- x! _6 X
'test_db',$id $ L1 J. O2 H E+ k( {9 Q+ Z1 ^
));
4 }7 ~4 J% \! m ^( Z, U7 J' l0 {" e4 F. P# k- e
DB::fetch_first('select * from %t where name=%s',array(
. H. y: a8 O; {'test_db','ccc'
3 l$ W. A2 O+ ~+ M) A# T));
5 @- j0 F/ x0 g9 e p& G
3 P. ^3 q* W- P3 n" P$ H8 L, E) f4 A3 n0 ^6 s( f$ L) i, @
x' ^2 M7 Q; B" w
数据表查询(多条):
9 w5 F& s5 \) Z/ A- ]$ v6 W; |7 _方法:
; z" y" j0 ?) {9 eDB::fetch_all() * [& X5 c& u/ _! t* t) N% w
参数:
5 a/ ?, `% {9 [$ S, S3 ^8 U3 x$sql:查询数据的SQL语句
+ j/ R+ w. f6 x/ _+ @3 _$arg:绑定查询的参数 . g1 F6 W9 N% Z
$keyfield:一维索引的字段名称 * e. ^) L7 L- t
$silent:查询失败是否不提示 ' k( G2 s( ?* H S) [5 t, l y
实例:
' I8 w. v" P6 @/ NDB::fetch_all('select * from %t where id >= %d AND id<=%d',array(
( n7 G, l1 s8 C. W0 c'test_db',7,10
4 ^) j6 h) n% E6 m6 n5 F! a));
$ j; @/ t) C3 y! I& e4 a9 Z9 A5 {8 o/ n3 c! B1 H, P% j
DB::fetch_all('select * from %t where id IN (%n)',array(
$ m! Z* S7 a- z% q; h M- F'test_db',array(1,2,3,4,5) 7 r) p* d9 [2 s; T
));
' F4 }8 g* `0 \. A8 b9 W( D0 L1 A7 m/ s2 o. s& [( p
数据表查询(单字段) 常用于统计查询: $ X2 \) w* b" k! A
方法: 2 s* i; m* A6 Q8 O1 x* p* M! x
DB::result_first() $ u( ]; f5 A6 W; y1 `' g+ y& ^
参数: + w* p5 D" S; T% a. N D
$sql:查询数据的SQL语句
/ a: O% V- l1 n$ I$arg:绑定查询的参数 9 z' y9 V# w7 |2 T
$silent:查询失败是否不提示
0 @5 @- V; z e& e% {5 e( q% B$ X实例: ' B" G/ i8 P7 k. H; y$ x
DB::result_first('select name from %t where id=%d',array( % f. z4 t" X! N$ n' k
'test_db',1
: F5 i6 C2 i4 l* ^' `% t t" b6 d6 i)); # i, ?4 e) \( A' k9 D
; M2 d$ N; ?, o5 [7 c
DB::result_first('select max(id) from %t',array(
/ H& [6 D' c7 x, L6 S'test_db' : [9 p( E$ t& m% E; a8 ~" w
)); 9 X4 k( H# G3 b4 t1 M. X
( s* m$ C$ ~( V* U, N3 f5 i) W l/ A
数据库自定义query 6 x# M4 @3 E( j9 h
方法:
2 N/ C. v$ j3 C- b i2 _5 H' ZDB::query()
8 e# r, S- P8 }, o$ o* C4 Y参数:
- d' o1 Z$ U E3 ]% Z$ U- P) C9 x$sql:自定义SQL语句 4 c& a N% m9 \4 W2 S; e
$arg:需要绑定的数据
9 q1 b5 {+ X3 V+ J0 C' ]$silent:执行SQL失败时,是否不提示
4 d; D+ e: Z$ Y. u }' R$unbufered:是否使用无缓存查询
+ B/ U# D3 q( D实例:
. ] N$ b0 a9 P+ }$ h, a$query = DB::query("select * from %t where id IN (%n)",array(
5 H7 H3 `. w1 L3 G'test_db',array(1,2,3)
' P: c1 S8 F. m8 c% ]$ I)); , H9 n; L) D$ x$ e: [0 Z
debug($query); 2 f) G: e* S& }- e1 b# u
1 _! E# j% m* P% t$ c! C删除 4 z/ x2 f! k) @; u
DB::query("delete from %t where id = %id",array(
4 a8 @8 x. L8 e% x$ M0 Q'test_db',11 5 E1 {6 I, O4 ^8 z
)); k2 q1 H* t* L m3 Q
8 O1 V0 n7 l2 t, ^% q6 ?2 d; h) F
5 f1 E. G, {$ m @* s) o9 X; |资源集转换结果集
) t2 |& g1 p" J f* B方法:
0 Y7 M; T" D# iDB::fetch()
2 U* U( Y. q# O+ K参数:
9 _: ]; i; _, l; w' S1 v- b$resourceid:数据库查询的query资源
) L+ {) z& r! e) E$type:数组类型
2 A4 ~1 k9 _3 ]& Y% Q. |; v实例:
' W( m2 c3 b2 q查询 3 w6 }+ A5 p7 z) Q
$query = DB::query("select * from %t where id IN (%n)",array( 8 `) C* z T2 _9 B! S4 E1 y7 D
'test_db',array(1,2,3)
: S2 c% Z) L$ d8 T5 u)); - }3 W2 o7 p, d5 ]6 O' x; x
while($res = DB::fetch($query)){ - |8 m/ {% e7 q+ P3 A' S
$result[] = $res; , ?* V8 ]7 h9 h" k4 z
}
/ W* b: B+ m/ S# bdebug($query);
) C6 t P4 u& }& q7 m1 h( k
% C3 p- m3 x; z单字段资源集转换结果集(一般用于查看获取统计值)
, `+ i- ^, I: H6 O% s方法:
) j Z @ I$ P$ gDB::result() 7 n" A' M( r" [3 U
参数:
- `* j! y' N3 K/ P" I8 w% E- W$resourceid:数据库查询的query资源
! G2 z4 {2 g( D$row:指定行的第一个字段 # n' v5 t/ l N. `
实例:
3 F8 H7 l7 W* ~& n) r1 ]& Y6 K$query = DB::query("select count(*) from %t where id < %id order by id",array( $ C6 l4 u/ @ e/ p, @$ ^
'test_db',10 / f# I- n: j* J% K, j3 {/ j
));
% x4 v9 u( a1 e3 L$data = DB::result($query,1);
# K+ F1 \' j( t) U% `
7 v7 s' z: m$ L# C+ i" q常用实例:
* u9 t2 C6 t( B9 A h$query = DB::query("select count(*) from %t where id > %d",array( 5 c5 G% l& r: N# f/ v( Y- K
'test_db',7
6 ?$ K5 z& f2 Z0 D7 d: Z+ H));
3 J7 V7 w$ ^( U) o) a$data = DB::result($query,0); 5 x4 X: d! F, N! ^
echo $data; ( O0 O" v% ?% b. s9 ]
& j7 T5 T$ z& \' c
资源集行数计算(不适合统计太多的资源,速度慢)
6 X$ Q& W7 v+ i' N$ n/ V A方法:
- B* m: f$ M. G) H; g( {' {, ^DB::num_rows()
4 n9 }0 t l2 z参数: , e4 m/ J1 a, [8 Q# M7 O
$resourceid:数据库查询的query资源 + P) ]* I8 `0 C5 h* `
实例:
. n% k* p# h0 q/ I; O+ b$ f. S$query = DB::query("select * from %t where id > %d",array( " t. [3 J* L2 }$ d
'test_db',7 & w2 _5 h: s+ [; x
));
) m4 h" \- e/ E* {$data = DB::num_rows($query); 9 t: o3 A5 u! D% k9 i/ J3 T
echo $data; , | a2 k) _8 \$ O! I q8 U5 h V# ?
$ i0 D4 N* C" w( r/ h1 q资源集资源释放
3 y8 t3 V2 {1 |+ _方法:
: Q) b5 F5 m# s; kDB::free_result() $ J* `8 W+ ?4 \: d( w* o& _: O1 J
参数:
3 W0 P# S; H) Y2 y+ l! Q$query:执行SQL语句的query资源 3 o1 Y/ g& X& A6 L' ^- a
$query = DB::query("select count(*) from %t where id > %d",array( " U: Z o* a/ u% W/ m
'test_db',7
' M- V" m$ ~" t8 u% `& E9 q));
9 u) `& Z' k# ]9 r; [$data = DB::result($query,0);
" a; E7 i9 _: A* g& ^7 v' gDB::free_result($query);
2 ~8 w/ t0 q, Y( T& \' S* E' `echo $data;
3 z2 }" S0 Q0 @- Q9 ~1 _' [
- ^9 Y7 Y$ u1 o: l7 T& q b按字段排序 0 p. p9 c9 A/ e1 t# P
方法:
$ i: [& F: C2 J7 \7 @: fDB:  rder() N, f" v8 G3 e. b! M
参数: % C2 W/ b, _. g# t* x
$field:需要排序的字段 # X9 A! _ d$ ]: ~. w) I" h! q9 ?
$order:排序方式
: a7 c, U& Y& z2 ^- G3 S实例:
. M; ^& D( V+ x. q* \1 D- [* }$query = DB::query("select * from %t where id > %d order by".DB:  rder('id','desc'),array( . Z U! m% T8 L3 R) G& E1 z
'test_db',7
. t2 b# _& m& y @) B6 g));
8 l7 s/ D& _' {6 k$data = DB::result($query,0); 0 T5 P6 ~9 R: O
DB::free_result($query);
/ W, l3 ?* A/ I% b: Decho $data; * p+ A+ M i h; }: D
1 Q! G, N3 } _
取值区间设定 . d% U" b/ R3 ~! D! p5 L. A3 `6 M
方法:
. S# `0 H6 g+ c% J4 KDB::query()
+ L; Z9 ^) H$ `& A& v& c参数:
" |% o5 T# J9 E$start:开始的索引值
; ~$ O1 E; D. f S$limit:条目数 ?8 E/ {' [5 ~
$query = DB::query("select * from %t where id > %d order by".DB:  rder('id').DB::limit(0,3),array(
% T9 `/ j% a W) j; b'test_db',7
- Q9 x) x, i$ K1 q)); 2 q' \: U/ e, G9 \
7 b3 a0 Z. N/ R2 w
字段拼接 : C+ N/ n i2 Q$ f+ o
方法: * H4 g4 C" ` q j: _
DB::implode()
9 @; ?3 r, m R8 f& X9 L3 D参数: p) J1 r5 H5 J% Z
$array:需要拼接的字段数组 # ?- ^) e0 C5 V2 w2 F4 O# Q% J
$glue:字段拼接的字符串 2 v; t! v* i" B) t/ v
实例: # Y! g1 J& |( g& v6 j$ c" }
echo DB::implode(array('id' => 10,'name' => 'ddd'),'and');
! D4 [! e, z% ^* ~5 B) m4 G0 G5 Q1 `( S9 j$ r2 J q
应用:
* U+ y: @# k g5 p; K9 [0 MDB::query("update %t set".DB::implode(array( ) P4 y( E4 T+ C7 J/ M
'name' => 'ddd',
4 l* R+ m2 ?$ a W. M+ _) B+ Q'id' => 22 3 L/ ]3 N. E# G( J# r
)).'where id=%d',array( # i3 t8 J' S) W* S" P/ v h7 ~
'test_db', - r/ F: V0 i4 R! x
10,
6 a% h w( N: h4 r& s0 W1 b0 z) % c4 z( ~" s5 z& u7 |2 G
); 6 n3 c' _$ o; d. W
8 L. w! ]' n2 D9 S
字段数据设定
/ h7 D [+ Z& \9 W0 J0 R6 v; o2 C方法:
" H7 c& f3 p$ \9 m7 O, m+ z8 UDB::field()
4 a, H8 ]7 @2 Z( b% U3 Q参数:
s: T" {$ R0 h6 _: i p" k$field:需要排序的字段
; v, S& y2 X# [; }9 S$val:字段对应的值
# G8 x; ?" Z- j" n3 ^$ }, N" H$glue:连接字段与值的类型 3 i( @) p, F9 x! @& q; v. m
实例: / s7 t, O" T0 z9 E8 p. Y
DB::field('id', 99, '=');
8 h5 A1 S& P: D( l. a/ i% I3 ?1 r, n, T
应用(把3改成99): # U. O* Q2 G6 S: i6 K1 ?" H$ \
DB::query("update %t set ".DB::field('id', '99', =)." where id=%d",array( ) w' f. }, {; z* X7 @7 C
array('test_db', 3); 4 q# E- K# q `, W5 C% N
));
/ z. _* _% O1 N- J: F; b
|
|