1、mysql_connect()-建立数据库连接
{3RY4HVT?
格式:
Fv n:V/eb
resource mysql_connect([string hostname [:port] [:/path/to/socket] [, string username] [, string password]])
_I;+p eq
例:
1(V>8}zn
$conn = @mysql_connect("localhost", "username", "password") or dir("不能连接到Mysql Server");
LqnN5l@ _B
说明:使用该连接必须显示的关闭连接
!mFx= +
hQ!slO
2、mysql_pconnect()-建立数据库连接
F.P4c:GD
格式:
=$`xis/
resource mysql_pconnect([string hostname [:port] [:/path/to/socket] [, string username] [, string password]])
KjR^6v
例:
/VL[,z=q.
$conn = @mysql_pconnect("localhost", "username", "password") or dir("不能连接到Mysql Server");
19od# d3+
说明:使用该连接函数不需要显示的关闭连接,它相当于使用了连接池
bH}6N>Fp
b&.j>=
3、mysql_close()-关闭数据库连接
%1Gat6V<'
例:
1 h(oty2p
$conn = @mysql_connect("localhost", "username", "password") or die("不能连接到Mysql Server");
S4]}/Imn)
@mysql_select_db("MyDatabase") or die("不能选择这个数据库,或数据库不存在");
/60 `"xH
echo "你已经连接到MyDatabase数据库";
Sbub|
mysql_close();
Ui6f>0?
@MIBW)P<
4、mysql_select_db()-选择数据库
j&(2ze:=*$
格式:
?qr-t+
boolean mysql_select_db(string db_name [, resource link_id])
loVUB'OSv
例:
$M/|zUQu.
$conn = @mysql_connect("localhost", "username", "password") or die("不能连接到Mysql Server");
m#SDB6l
@mysql_select_db("MyDatabase") or die("不能选择这个数据库,或数据库不存在");
"&/:"~r
,eq[X/B>
5、mysql_query()-查询MySQL
)&di c6r
格式:
?Dro)fH1
resource mysql_query (string query, [resource link_id])
z'a#lA.$}
例:
$Gb] K{e
$linkId = @mysql_connect("localhost", "username", "password") or die("不能连接到Mysql Server");
Ju$=Tn
@mysql_select_db("MyDatabase") or die("不能选择这个数据库,或者数据库不存在");
Bb[%?~ E!
$query = "select * from MyTable";
" 6 /`
$result = mysql_query($query);
n k]tq3.[
mysql_close();
$|.x!sA
说明:若SQL查询执行成功,则返回资源标识符,失败时返回FALSE。若执行更新成功,则返回TRUE,否则返回FALSE
{= Dtajz
QC0^G,9.
6、mysql_db_query()-查询MySQL
b Jt397
格式:
/7Hzj0hSi
resource mysql_db_query(string database, string query [, resource link_id])
u`Djle
例:
~k+"!'1
$linkId = @mysql_connect("localhost", "username", "password") or die("不能连接到MysqlServer");
/6}4<~~4TA
$query = "select * from MyTable";
/b,+YyWi%
$result = mysql_db_query("MyDatabase", $query);
E :*!an
mysql_close();
qF=D,Dlz
说明:为了使代码清晰,不推荐使用这个函数调用
rb%P30qc4
V1Dwh@iS
7、mysql_result()-获取和显示数据
(V @g?|LZ
格式:
i+T#z
mixed mysql_result (resource result_set, int row [, mixed field])
;[WW,,!Y
例:
%lz/w{
$query = "select id, name from MyTable order by name";
]]@jvU_?kS
$result = mysql_query($query);
W3* BdpTw
for($count=0;$count<=mysql_numrows($result);$count++)
leyhiL<
{
$c_id = mysql_result($result, 0, "id");
Yo @>O98
$c_name = mysql_result($result, 0, "name");
:k8>)x] )
echo $c_id,$c_name;
S%uH*&`
}
aq~hl7MTj
说明:最简单、也是效率最低的数据获取函数
2NAGXWE
l_iucN
8、mysql_fetch_row()-获取和显示数据
_CAW D;P
格式:
B)/X:[
array mysql_fetch_row (resource result_set)
)5gcLD/zI
例:
^"v~hjM#
$query = "select id, name from MyTable order by name";
fL ng[&
$result = mysql_query($query);
^ $Q',
while (list($id, $name) = mysql_fetch_row($result)) {
X'.}#R1
echo("Name: $name ($id)
");
l"RX`N@In
}
PKT0Drv}c7
说明:函数从result_set中获取整个数据行,将值放在一个索引数组中。通常会结使list()函数使用
8Fx~i#FT
f}^}d"&F
9、mysql_fetch_array()-获取和显示数据
uzG<(Q pu
格式:
WRZi^B8 @
array mysql_fetch_array (resource result_set [, int result_type])
;j=/2vU~@
例:
,& =(DJ
$query = "select id, name from MyTable order by name";
_7~O>.
$result = mysql_query($query);
S0/@y'q3en
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
+N"A5U
$id = $row["id"];
,d)!&y
$name = $row["name"];
xU@YBzbk
echo "Name: $name ($id)
";
{,*G }/9<
}
.oH0yNFX
又例:
;WJ}zjo >
$query = "select id, name from MyTable order by name";
qW6a|s0}
$result = mysql_query($query);
d,iW#,
while($row = mysql_fetch_array($result, MYSQL_NUM)) {
@}!1Uk3ud
$id = $row[0];
+8."z"i3lE
$name = $row[1];
/>Tyiy]2uu
echo "Name: $name ($id)
";
AW&HWc~A
}
/S$p_7N
说明:
.6xP>!E}Q
result_type的值有:
a)MjX
MYSQL_ASSOC: 字段名表示键,字段内容为值
,]i ^/fT
MYSQL_NUM: 数值索引数组,操作与mysql_fetch_ros()函数一样
l2+qP{_4
MYSQL_BOTH: 即作为关联数组又作为数值索引数组返回。result_type的默认值。
pM@|P,w {
Z_}vjk~s
10、mysql_fetch_assoc()-获取和显示数据
pB|L%#.cW
格式:
PYkcGtVa_
array mysql_fetch_assoc (resource result_set)
5UVQ48aT
相当于调用 mysql_fetch_array(resource, MYSQL_ASSOC);
U;D!m+.HK
{lT9gJ+
11、mysql_fetch_object()-获取和显示数据
H4 Ca+;
格式:
ik+qx~+`Qv
object mysql_fetch_object(resource result_set)
w (W+Y+up
例:
[3nhf
$query = "select id, name from MyTable order by name";
W'E!5T^
while ($row = mysql_fetch_object($result)) {
C?#if;c
$id = $row->id;
N6c']!aM@
$name = $row->name;
g.kpUs
echo "Name: $name ($id)
";
D%LqLLD
}
{:IOTy
说明:返回一个对象,在操作上与mysql_fetch_array()相同
CM>/b3nOW
@zEEX9U
12、mysql_num_rows()-所选择的记录的个数
i)1E[jc{p!
格式:
GI WgfE?
int mysql_num_rows(resource result_set)
!3DWz6u
例:
~=Z&l
query = "select id, name from MyTable where id > 65";
iH9g5G`O
$result = mysql_query($query);
%;!@/5$
echo "有".mysql_num_rows($result)."条记录的ID大于65";
FZ #ngrT
说明:只在确定select查询所获取的记录数时才有用。
'AF2:T/
6]/F_Z41
13、mysql_affected_rows()-受Insert,update,delete影响的记录的个数
Sr)/ Mf
格式:
X@b$C~+
int mysql_affected_rows([resource link_id])
0Q;T <% U
例:
UQ$dO2^
$query = "update MyTable set name='CheneyFu' where id>=5";
ci+Pg9sS
$result = mysql_query($query);
,M>W)TSH
echo "ID大于等于5的名称被更新了的记录数:".mysql_affected_rows();
Vt5%A}.VQ
说明:该函数获取受INSERT,UPDATE或DELETE更新语句影响的行数
A%"XNk
#dqZdj@
14、mysql_list_dbs()-获取数据库列表信息
U# -&%|b$
格式:
hEl)BRJ
resource mysql_list_dbs([resource link_id])
lBR6O!sBP
例:
)3.udx
mysql_connect("localhost", "username", "password");
Wv77ef
$dbs = mysql_list_dbs();
Y3-Tg~/~W
echo "Databases:
";
1|;WaO1Q
while (list($db) = mysql_fetch_rows($dbs)) {
m,K/e
echo "$db
";
C0 .Xp
}
[_|i W%<`
说明:显示所有数据库名称
'&?47+W
oFoG+H"&7/
15、mysql_db_name()-获取数据库名
56>Zqtp*
格式:
e:AB!k^xp$
string mysql_db_name(resource result_set, integer index)
9`{Mq9J
说明:该函数获取在mysql_list_dbs()所返回result_set中位于指定index索引的数据库名
+P~zn=
t82Bp[t
16、mysql_list_tables()-获取数据库表列表
O5_[T43
格式:
tK g%5;v
resource mysql_list_tables(string database [, resource link_id])
Cla Yy58v
例:
@F=ZGmq
mysql_connect("localhost", "username", "password");
P M9HfQU?
$tables = mysql_list_tables("MyDatabase");
d>@{!c-
while (list($table) = mysql_fetch_row($tables)) {
-
echo "$table
";
u+(e,t
}
9tl Fbu
说明:该函数获取database中所有表的表名
i;6/tK"!
Y8/Ms^rz
17、mysql_tablename()-获取某个数据库表名
-d_FB?X
格式:
k^R>xV
string mysql_tablename(resource result_set, integer index)
;1(OC-2>d
例:
xs.[]>nQN
mysql_connect("localhost", "username", "password");
[USXNe/
$tables = mysql_list_tables("MyDatabase");
KO{}+~,.6
$count = -1;
Kh$L~4l
while (++$count < mysql_numrows($tables)) {
dN/pe@#lKP
echo mysql_tablename($tables, $count)."
";
Ta9;;B?$
}
.rxc"fR4_
说明:该函数获取mysql_list_tables()所返回result_set中位于指定index索引的表名
"_H&p
!p0FJ].g,
18、mysql_fetch_field()-获取字段信息
K''b)v X4
格式:
Ir0er~f+z
object mysql_fetch_field(resource result [, int field_offset])
r9*{)"
例:
K.X% Q,XD
mysql_connect("localhost", "username", "password");
Nk=M
mysql_select_db("MyDatabase");
]'M4Unu#@
$query = "select * from MyTable";
oiFtPki
$result = mysql_query($query);
YVs{/1|'
$counts = mysql_num_fields($result);
!w C4ei`
for($count = 0; $count < $counts; $count++) {
}cS3mJ
$field = mysql_fetch_field($result, $count);
f(##P|3>R
echo "$field->name $field->type ($field->max_length)
";
iqOd]H]v
}
%mxG;w$
说明:
Z ,T TI>P
返回的对象共有12个对象属性:
9$)4C|
name: 字段名
Ue l*:c
table: 字段所在的表
9I pjY~or
max_length: 字段的最大长度
M rVtxzH
not_null: 如果字段不能为null,则为1,否则0
primary_key: 如果字段为主键,则为1,否则0
2QBq
unique_key: 如果字段是唯一键,则为1, 否则0
t`Hwq
multiple_key: 如果字段为非唯一,则为1,否则0
7H./o Vl
numeric: 如果字段为数值则为1,否则0
. e]!i(5I
blob: 如果字段为BLOB则为1,否则为0
ggbew6L$Z
type: 字段的数据类型
98V9AOgk
unsigned: 如果字段为无符号数则为1,否则为0
h#`qEK&u
zerofill: 如果字段为“零填充”则为1, 否则为0
GMiWS:`;v`
ogQbST
19、mysql_num_fields()-获取查询的字段个数
kOkgsQQ
格式:
#n0P'@d,r
integer mysql_num_fields (resource result_set)
Eb#0 -I
例:
R%b,RH#
$query = "select id, name from MyTable order by name";
rK"$@ tc
$result = mysql_query($query);
JLs7[W)O
echo "这个查询的字段数是:".mysql_num_fields($result)."
";
-3bl !9h^
2MJ0[9
20、mysql_list_fields()-获取指定表的所有字段的字段名
K);:+s-
格式:
L5PN]<~T
resource mysql_list_fields (string database_name, string table_name [, resource link_id])
=Y /
例:
/Mt(9jNK
$fields = mysql_list_fields("MyDatabase", "MyTable");
^
echo "数据库MyDatabase中表MyTable的字段数: ".mysql_num_fields($fields)."
";
m"jqHGFV
nh,N (t 9
21、mysql_field_flags()-获取指定的字段选项
y%3Yr?]
格式:
%6*xnB?
string mysql_field_flags (resource result_set, integer field_offset)
z
例:
gdHPi;
$query = "select id, name from MyTable order by name";
K4A=lD+
$result = mysql_query($query);
2h u;N
$row=mysql_fetch_wor($row);
k/1q Jr
ezy0m}@
22、mysql_field_len()-获取指定的字段的最大长度
Z2hRTJJ[A
格式:
W=]QTx,J
integer mysql_field_len (resource result_set, integer field_offset)
)2u=U9
例:
zj"J~s;?
$query = "select name from MyTable";
ZuILDevMD
$result = mysql_query($query);
n%W~+
$row = mysql_fetch_row($result);
o= N=W
echo mysql_field_len($result, 0)."
";
uRwIxT2
说明:
)O2giVq7[0
如果mysql_field_len($reseult, 0) = 16777215
Yb 6(KT
那么numer_format(mysql_field_len($result))等于16,777,215
jK3% /`o
:v8j3=
23、mysql_field_name()-获取字段名
W}.;]x%1B
格式:
)r5QOa/
string mysql_field_name (resource result_set, int field_offset)
&*/8Ojv)9
例:
M[@).4h
$query = "select id as PKID, name from MyTable order by name";
4 Z1- RS
$result = mysql_query($query);
9Clddjf?c
$row = mysql_fetch_row($result);
F+v?2|03
echo mysql_field_name($result, 0); // Result: PKID
!7P 1%/
K({+3vK
24、mysql_field_type()-获取字段类型
CKmoC0.
格式:
q --NLm@;
string mysql_field_type (resource result_set, int field_offset)
|wQ|h$|
例:
u&m B;:&
$query = "select id, name from MyTable order by name";
>-c;
$result = mysql_query($query);
JyvXNV,
$row = mysql_fetch_row($result);
? D2:'gg
echo mysql_field_type($result, 0); // Result: int
B=JeZMn
i:ZpAo+Z{
25、mysql_field_table()-获取字段所在表名
kQRNVdiz
格式:
Xv|~1v%s7
string mysql_field_table (resource result_set, int field_offset)
#(?EL@5
例:
bP`yLz
$query = "select id as PKID, name from MyTable order by name";
X48Q{E+
$result = mysql_query($query);
4xjk^N9
$row = mysql_fetch_row($result);
/=?f4*4|/
echo mysql_field_table($result, 0); // Result: MyTable
_1VtVfiZ{