数据表插入:
! s6 N+ H& d$ d2 y' C& k方法:
: Y6 h3 R3 T8 V) c+ }/ ADB::insert() 0 J6 ~) B( \* u& _
参数:
: @8 Z3 v3 p2 w4 @" r# ~. O$table:插入数据的表 8 a, M$ n$ T7 g7 p! }) ]* t
$data:插入的数据,字段对应值
9 d, l1 `, q( [7 y& R6 S$return_insert_id:是否返回插入数据的ID " |. n5 v/ l9 f' _' w3 N# p
$replace:是否使用replace into(当原始表中存在不重复数据就插入,存在就更新)
8 v9 ~: f- ~% C# D$silent:操作失败是否不提示 ! @1 Q# m2 G) M9 m8 M$ M
. L a7 D5 `* |5 r! N$ B/ U实例 7 |3 w8 H" X4 s
DB::insert('test_db',array( . [9 S. y3 [9 V6 e. |
'name' => 'ppc',
* D, s& Q, D7 j5 Q1 r, k),true,true); 5 Y6 O0 e4 M* B# E
$ d* q, Y, f# Y$ E6 L& c数据表删除: , D5 }3 W$ n `( f2 Z* d( z
方法:
$ r. D% R, d: \4 p+ [DB::delete() # U7 U# I9 \0 N: |; z! y! c: d
参数: 4 X+ M" ~' {0 b, W
$table:删除数据的表 2 a+ v# v ?, k
$condition:删除的条件 5 d6 e9 i( D# J
$limit:删除满足条件的条目数
1 D& T2 m9 x' e$unbufered:是否使用无缓存查询
- O5 z( V5 P& b/ b( K+ W% ^2 ?/ Y9 T2 W: B% g L
实例:
! D7 u6 T4 z. |DB::delete('test_db','id>2',1,true);m 5 }. a/ ?" s# O
8 K0 [5 I2 e4 D
数据表更新: 8 i6 ~& D8 j( ]. G: ]( p
方法: . i3 `* h7 {- p
DB::update() ) _# d/ G4 u2 f4 j' y' L; E
参数:
0 b6 F. E& o0 d$table:更新数据的表 2 E! |5 y) }6 j3 _5 m4 C [
$data:更新的数据,字段对应值 . |& u: q7 {' ~ b
$condition:更新的条件
8 s$ ~5 Z. e; o( A$unbufered:是否使用无缓存查询
" r+ v2 U0 J8 V$ T1 ~$low_priority:是否采用无锁表更新 4 r N6 B2 \' E& T) h
3 [( p" W4 j) j1 j数据表查询(单条):
! b" ]$ @7 P y7 O" M+ T# C3 O# C方法:
) |! V8 t$ P4 ^3 a+ A* gDB::fetch_first() * k2 f ^/ y. J- x6 L3 V9 c
参数:
7 _( _. m0 `) a+ t; l$sql:查询数据的SQL语句 $ s: @7 q+ n' V1 `. [% B, M
$arg:绑定查询的参数 8 E" s5 j: W. N* f8 ]5 X
$silent:查询失败是否不提示 2 ?+ o3 N6 P0 i; `- c2 o
实例:
; O* C1 ]( }7 I2 K: @$id = $_GET['id']; ! Y) X) R; m$ r$ n3 l
DB::fetch_first('select * from %t where id=%d',array(
4 E5 T) h( V3 L2 J- F2 L; J'test_db',$id ) q. ^4 G) f) U; f
));
, m5 O/ `1 k; B7 D! [8 e% H" x7 M" j4 N
DB::fetch_first('select * from %t where name=%s',array(
2 w, T% A! i$ X) \; W6 ^'test_db','ccc' 4 q& M4 z$ f( N& F9 z7 A: M1 ~
)); / Y* r( p7 g: E! d0 O
( c L O1 u( L& f9 Z0 b& i
9 f4 E# y- ?% ~$ I+ H: Z
* s+ y+ D6 k+ H$ r
数据表查询(多条): 4 l$ d! Q0 t1 T: X& u6 S7 @
方法:
9 L6 \$ b9 d; `* b; QDB::fetch_all()
* d! G; N1 V/ v- E2 Z8 z参数: " r$ O0 X& B' N2 q5 W2 F- @! l
$sql:查询数据的SQL语句 ! h: M9 n. K$ [! f
$arg:绑定查询的参数
$ P- i3 @! r" d! B" z$keyfield:一维索引的字段名称 6 g7 n; I4 g" T' {4 }; P
$silent:查询失败是否不提示
( b4 d" C; H6 b3 ?实例:
3 m1 E3 l; d5 y6 K BDB::fetch_all('select * from %t where id >= %d AND id<=%d',array(
: J+ T: m" K% x7 E; n0 Y: x'test_db',7,10
" @# |& a; G# t* a" z: _)); & H1 I5 f! f! C U( m9 C: X
9 v _$ f! S1 D) @: f( a- u7 D( JDB::fetch_all('select * from %t where id IN (%n)',array(
% d0 o$ ~9 U- l( M& O |'test_db',array(1,2,3,4,5)
& g8 K8 g4 W8 F& T6 x: F8 q( n));
( i" R. S. |5 Z$ {1 f) A4 \- u: C4 e( A
数据表查询(单字段) 常用于统计查询:
9 ?5 m8 U6 m" O7 T; ]' y+ T方法: ! t& V( ?% D: F7 O
DB::result_first() 8 [' Y3 w- e F* E4 F
参数: 1 f$ y9 o4 @- q! {
$sql:查询数据的SQL语句 " i$ t, j- g/ c: E+ W+ h z* \
$arg:绑定查询的参数
% U3 J5 R. E4 O, a% M$silent:查询失败是否不提示
, v8 `) d1 x# ?5 p5 O实例: : _: ^( d( R+ G4 o
DB::result_first('select name from %t where id=%d',array( q$ V8 C- ~+ L0 @1 b
'test_db',1
8 L. p( T% U! q; `; B* V));
7 w" D4 v( _' d8 c+ _/ z! s" m9 V" m% n
DB::result_first('select max(id) from %t',array( 5 }& \ {2 b0 m. F0 `# @
'test_db'
( n3 f, |% z9 I/ B0 Z' ?)); $ ~( ^2 Q! ?0 }( N: ?1 w" O
! G5 X+ h5 G9 L) j/ H数据库自定义query
' J3 G, T: u7 V& S3 B; X( V方法:
+ l2 x& e% r! }% G6 C) p/ @6 PDB::query()
" P' k, Z2 A% U) t- K参数: , [7 }" ]7 A! o; f- o
$sql:自定义SQL语句 - H. D8 j/ y3 r% ^; v# P
$arg:需要绑定的数据 ) I' D4 W* n0 {2 F
$silent:执行SQL失败时,是否不提示
' G: F+ G3 j+ w# P$unbufered:是否使用无缓存查询
( T+ Q4 U' E/ O5 _3 W( K# ~2 v4 j实例: 4 q3 o' j0 L) Q' l
$query = DB::query("select * from %t where id IN (%n)",array( 7 `* V; e$ N7 q. W( A( b
'test_db',array(1,2,3)
+ G$ S5 v2 ^8 y% G));
" s- n! r; R, S: Ndebug($query);
$ C4 o! T% e' T" a/ F* A: C, R* {9 A) l7 K5 m9 v
删除
, ?0 R% `9 W: vDB::query("delete from %t where id = %id",array( % c, }/ T- Q5 @) Z' k
'test_db',11 ) N2 s7 }5 L% y! {
));
3 [ [6 H( K7 w+ q, n+ Y& l" T p e Z9 v6 B% c" N1 S" i
& A2 L2 H) X/ N' B; h0 Z* D资源集转换结果集
8 h: f9 z& k4 j9 t2 y方法:
# F2 W, t5 Y8 j/ p3 }DB::fetch() * U; I& L; x: T6 q7 m: o7 n
参数:
" r& d9 D# _* l. q0 `2 m1 H1 q$resourceid:数据库查询的query资源 3 |# ]6 S; M6 f& c( y1 ]& p
$type:数组类型 # z/ i! x4 L0 D4 m3 X
实例: 8 { ]9 C' X/ z: g- j0 b' ? _
查询 , W% S( j- v. G& A
$query = DB::query("select * from %t where id IN (%n)",array(
4 [4 ~' F n4 @) B( R0 i7 ]'test_db',array(1,2,3)
& l! Z5 Y, ]6 R/ r)); ( n. n; R8 r2 C* L( n9 r* |. W1 p
while($res = DB::fetch($query)){ 9 \# }! ?$ K' l8 s9 u
$result[] = $res;
- ?$ b( ^3 H& T# [. {0 ~} . P- ?1 o6 ?) w6 N( k* N g
debug($query); : c0 u+ B4 r5 \1 _; y2 e* j
+ }' U6 f* B: M) K1 ~单字段资源集转换结果集(一般用于查看获取统计值) ) p$ [* Y- Z+ _: w. w! B
方法:
: [" N0 I2 Y/ Y9 b/ {8 u# TDB::result() 2 y* M6 s. ?7 M: D6 z5 s$ Q& ]
参数:
) g' @/ U- E, k9 m$resourceid:数据库查询的query资源
2 z5 x0 B) \$ a- o$row:指定行的第一个字段
+ _( J5 i5 L) U* G& S E实例:
7 d8 Y! F) ^& R$query = DB::query("select count(*) from %t where id < %id order by id",array(
, S5 G9 a0 m9 L, H- R0 I'test_db',10 " R7 Q( p6 n! K* Z: ]4 o
));
0 W9 H, H% P) Y$data = DB::result($query,1); ) H8 G2 h7 G5 n: F/ {; Z# x0 w
|) d6 G, C, B+ h' d, Y4 \& m. E常用实例: ; k) l( r2 ?( G: H9 c" w
$query = DB::query("select count(*) from %t where id > %d",array( 2 ~$ J% a' p% Y6 Y
'test_db',7 3 y) ?6 Y; I z6 U* |3 z
));
& `# f& H- q4 w$data = DB::result($query,0);
8 s4 h2 }- Z6 t% Lecho $data;
! w8 y" _3 f1 _: O
% h3 ~( M1 L% o! s资源集行数计算(不适合统计太多的资源,速度慢) 3 c2 ]/ d# G2 K n+ x9 B) G3 z
方法: ! O! e0 |8 ^' e9 U
DB::num_rows() 7 z) K0 g9 B/ t2 k( h% [( W( g
参数: / b! j1 X- A3 y4 Y; p
$resourceid:数据库查询的query资源 . @" s. o2 ~8 b1 o
实例: # I- C: B; ]$ B2 k# P
$query = DB::query("select * from %t where id > %d",array(
8 }* S" ~7 Z) {'test_db',7
6 r- ]: p M* }7 X ~3 O));
- _$ x# m8 l. V7 ~' v) }, U$data = DB::num_rows($query); . o" n; L) Q. t( i
echo $data; }, t8 x# X. v6 C+ G# n' h( m
& `5 d1 ~$ b: K资源集资源释放
) V0 O" i( t6 w1 h. k方法: , \* v T% @; ~, ]. S$ [; w
DB::free_result() 8 ^9 o% i: Y2 H8 g8 m5 ~% s2 `# W
参数:
* a6 \% ?4 J8 S9 o$ u: L$query:执行SQL语句的query资源 - Q# P9 x* E; b4 W1 @
$query = DB::query("select count(*) from %t where id > %d",array(
' H9 F: \" H5 y. A'test_db',7
3 s' P' }% H, g: ~));
3 l3 a) F i) Q0 o( o ~; C0 P! [$data = DB::result($query,0); - Z5 o! P' y# d; h
DB::free_result($query); + s+ X% m; B. n r h( V0 z
echo $data;
' A) R$ {7 b: c5 v, p; y7 N/ W
# O5 r9 i8 ? U) d. h. n1 F0 f按字段排序
+ G H" w* |# w7 C1 V4 Y/ @方法:
: B& o+ x h qDB:  rder()
" W$ o1 {% ~* I6 U' E$ |参数:
( z* l5 k( y; b w, @0 J$field:需要排序的字段 4 `9 f4 J' }+ w1 V1 t+ q, [
$order:排序方式 1 Z; f4 Q B+ j' q
实例: , S9 y9 ~3 q! j# V3 G( h
$query = DB::query("select * from %t where id > %d order by".DB:  rder('id','desc'),array(
* T- i0 K' W2 \3 N9 J'test_db',7
% P6 o. U) J: d7 P& B9 Q8 [$ Y8 w( e! e3 ?));
* h. _) J+ r& D! e( Q5 z$data = DB::result($query,0); 5 Y" W5 A7 y7 k
DB::free_result($query);
# u4 m* y; h" C$ n" L8 ~4 Yecho $data;
. C. W6 w3 W0 O5 F$ R
! @) B) b+ a! O取值区间设定
( B& ^7 V L: ?6 ~- D- o0 f方法:
7 c9 Y5 V& c6 F4 ^! y Z2 f8 f" ^DB::query()
5 a9 M( i. {7 R$ g1 t# K; [8 y参数: ( e0 F' J/ V8 r9 r4 G$ n H
$start:开始的索引值 0 j6 P" D1 b& c
$limit:条目数
7 F/ F2 g: B- j, t6 T; B3 v _* P$query = DB::query("select * from %t where id > %d order by".DB:  rder('id').DB::limit(0,3),array( 1 Q& U6 l- V; b Y/ h
'test_db',7 ' e; Q- n; p1 h5 s3 h% `0 {
));
( t' M/ |. w4 y0 o. f1 ^. r B6 {# F9 d( p& R( n
字段拼接
( s+ ]. i. c! M# B1 W+ k方法: 6 i6 a$ t& x, _# [" H5 @6 I
DB::implode()
9 Y; b' y9 h0 X- y5 ^9 {6 {参数: % M% L: E( Q: x' u! f$ z
$array:需要拼接的字段数组 r( c, |2 F4 x! }( t% z
$glue:字段拼接的字符串
% f3 `; F; y& V& L实例:
' {$ H$ d/ H7 d' vecho DB::implode(array('id' => 10,'name' => 'ddd'),'and'); # l2 ]1 C2 [& e! k
' z0 Q9 @+ c6 U6 r应用: ! N4 y5 B; t7 k1 N+ `; ~- X. B
DB::query("update %t set".DB::implode(array(
' K- j- n+ x& H; K# v( \& b' B' B3 |'name' => 'ddd',
* b6 c4 M r( H k'id' => 22
+ {. i' \' @7 x/ |$ N)).'where id=%d',array(
& M$ c' L8 `) p# j* D4 F' H'test_db', " ?) T2 N, b% _- |+ }
10, 5 Y( m# _2 E2 s8 @, l( h
) 6 ~9 i/ A9 C, w* x- [! M
); 9 R4 o k& V2 M; N7 ^9 H6 U
% J. B5 |) X) d字段数据设定 + Q) y# l+ L$ k- a! g' w; A# E
方法:
& {% H# M2 K" oDB::field()
: u7 ]& `7 U- i2 [% q5 t: b; p参数:
6 L8 F' H0 |& S5 h( }$field:需要排序的字段
# R# L5 V2 m- i" ^0 _0 e$val:字段对应的值
6 Y& h2 i9 I2 O$glue:连接字段与值的类型
3 K. F) s( ~5 M$ U! W实例: - L) {' x A# V, z' \
DB::field('id', 99, '=');
* D. R8 A4 F V6 l- G8 x) M+ T" F* e
2 q* m& R2 [7 _( O! Z应用(把3改成99):
* @; ~& j: p& y7 j- SDB::query("update %t set ".DB::field('id', '99', =)." where id=%d",array( : b3 k2 r7 m4 Q9 D( s
array('test_db', 3); 5 e, P$ w E3 d1 E6 j# n1 b6 A5 o
)); 6 ~6 {/ r, [0 }$ F; z+ l6 v# N
|
|