php show函数,PHP中的常用MYSQL函数 - msnshow的个人空间

  1、mysql_connect()-建立数据库连接51Testing软件测试网6p A4B4F$f

  格式:M-k0u-u4t+J

  K0

  resource mysql_connect([string hostname [:port] [:/path/to/socket] [, string username] [, string password]])51Testing软件测试网x:LP+RM)k

  例:"b@4Z:vUd'C,DIM0

  $conn = @mysql_connect("localhost", "username", "password") or die("不能连接到Mysql Server");|3v-^4dz-wP0

  说明:使用该连接必须显示的关闭连接51Testing软件测试网(y{qP/T@"p;T

  2、mysql_pconnect()-建立数据库连接AA3K0^T)Z9}Y0

  格式:1]`)V%-_4U0

  resource mysql_pconnect([string hostname [:port] [:/path/to/socket] [, string username] [, string password]])51Testing软件测试网prmAj&SU;Q

  例:+Tc1jA9E(Dp$pLs d-p0

  $conn = @mysql_pconnect("localhost", "username", "password") or dir("不能连接到Mysql Server");51Testing软件测试网&[ a^qeV!eLt

  说明:使用该连接函数不需要显示的关闭连接,它相当于使用了连接池i/Mr5on0

  3、mysql_close()-关闭数据库连接rE?+e ?-Oo-G0

  例:51Testing软件测试网4GeJ7nm

  $conn = @mysql_connect("localhost", "username", "password") or die("不能连接到Mysql Server");51Testing软件测试网#Wq.j&C%X

  @mysql_select_db("MyDatabase") or die("不能选择这个数据库,或数据库不存在");51Testing软件测试网*nL#gc B^:tq

  echo "你已经连接到MyDatabase数据库";0lp+q|.v4V/i@0

  mysql_close();L85eK"L3qT"y0

  4、mysql_select_db()-选择数据库2j*eo+Y}+^)Tl0

  格式:.y^l4V;gTL T$H0

  boolean mysql_select_db(string db_name [, resource link_id])M$iFu/^@0

  例:x}7_cB-H;q6g0

  $conn = @mysql_connect("localhost", "username", "password") or die("不能连接到Mysql Server");V+M9w4H e0w0

  @mysql_select_db("MyDatabase") or die("不能选择这个数据库,或数据库不存在");!N-}HQ;vb%fgO"T0

  5、mysql_query()-查询MySQL6nccI3d_ T0

  格式:51Testing软件测试网TZfg~

  resource mysql_query (string query, [resource link_id])51Testing软件测试网5W/wnD9i,u

  例:*gJ;i(gg5mU#tq3sbb0

  $linkId = @mysql_connect("localhost", "username", "password") or die("不能连接到Mysql Server");51Testing软件测试网^t[ T8W

  @mysql_select_db("MyDatabase") or die("不能选择这个数据库,或者数据库不存在");PmOSh@0q5k3u,v0

  $query = "select * from MyTable";51Testing软件测试网2Pdrd:N,U

  $result = mysql_query($query);~gT g1A s$F0

  mysql_close();51Testing软件测试网Oq&Av&XA

  说明:若SQL查询执行成功,则返回资源标识符,失败时返回FALSE。若执行更新成功,则返回TRUE,否则返回FALSE51Testing软件测试网ds,p3V8`*B3s1i4_

  6、mysql_db_query()-查询MySQL51Testing软件测试网|2x$@aa

  格式:-v1r(MAY.e.D0

  resource mysql_db_query(string database, string query [, resource link_id])51Testing软件测试网m9j%K3CwK"H

  例:51Testing软件测试网6`X6K6{I

  $linkId = @mysql_connect("localhost", "username", "password") or die("不能连接到MysqlServer");51Testing软件测试网_,S F,ze#_S iR

  $query = "select * from MyTable";51Testing软件测试网'B,EOiT&cj:W

  $result = mysql_db_query("MyDatabase", $query);51Testing软件测试网y+h(P

  rKT'J'X@w'_H

  mysql_close();51Testing软件测试网u&oX pXV8rI(M

  说明:为了使代码清晰,不推荐使用这个函数调用51Testing软件测试网$?4kE(t4Z-T#^$dy

  7、mysql_result()-获取和显示数据51Testing软件测试网~9G"Bh!O]xo

  格式:4W

  Q"X6lU,~'Z

  a NH0

  mixed mysql_result (resource result_set, int row [, mixed field])51Testing软件测试网ba5N"Ww0h

  例:2?Yf"?Zt0

  $query = "select id, name from MyTable order by name";2~.G0yZ_9Q:bBD0

  $result = mysql_query($query);51Testing软件测试网"M^j*O+PcN{2BuE+l

  for($count=0;$count<=mysql_numrows($result);$count++)51Testing软件测试网]]nY

  {51Testing软件测试网ONRuw#y-z

  $c_id = mysql_result($result, 0, "id");"d8V]N.q U?b0

  $c_name = mysql_result($result, 0, "name");51Testing软件测试网T#TW8C,wG

  G

  echo $c_id,$c_name;d;UT}Nifb&?0

  }9I&Frg(eeo0

  说明:最简单、也是效率最低的数据获取函数{N5wE[0

  8、mysql_fetch_row()-获取和显示数据51Testing软件测试网5_r.W7ANK%f

  格式:51Testing软件测试网B-Nr8P3GUt

  array mysql_fetch_row (resource result_set)|Qc6xQ*C0

  例:b.TQlpD2k0

  $query = "select id, name from MyTable order by name";#{s;~3CIO(F0

  $result = mysql_query($query);51Testing软件测试网qX}z0q)ErW

  while (list($id, $name) = mysql_fetch_row($result)) {/``!V!s!fi0}g0

  echo("Name: $name ($id)

  _.sj2y9K.BLz0");yNHez.Y^0

  }51Testing软件测试网n+f(X2V/g-d/ZS

  说明:函数从result_set中获取整个数据行,将值放在一个索引数组中。通常会结使list()函数使用_'k6l{8H2y0

  9、mysql_fetch_array()-获取和显示数据51Testing软件测试网yod.}7HC

  格式:?6D0U3`r0

  array mysql_fetch_array (resource result_set [, int result_type])51Testing软件测试网w[:[+^$~:q

  例:t-F?ZS6F0

  $query = "select id, name from MyTable order by name";51Testing软件测试网g0mg.Y|zsl(k

  $result = mysql_query($query);d7}"Y w/fR C0

  while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {51Testing软件测试网WWQ"w!}

  $id = $row["id"];51Testing软件测试网X#v/c*G&`'Xx

  $name = $row["name"];51Testing软件测试网X'a3KQ*O3I

  echo "Name: $name ($id)51Testing软件测试网_WF2U%Z1|gY!gwl

  ";p;N?$RB!x0

  }51Testing软件测试网0r?[8Nb5L

  又例:51Testing软件测试网2E;~5~@Fe{L

  $query = "select id, name from MyTable order by name";51Testing软件测试网.j

  J5Z6fo7?qy9p

  $result = mysql_query($query);~+WdpGg0

  while($row = mysql_fetch_array($result, MYSQL_NUM)) {X(Q9D1z;7m)oj0

  $id = $row[0];51Testing软件测试网L;o1q FP2c:YgT"MP

  $name = $row[1];#M G:z%??2@{0

  echo "Name: $name ($id)

  AWe3J+Cf0";.R UK `|(D U?> }0h%Jx1o+X0

  说明:51Testing软件测试网 _"S+LtZ

  result_type的值有:] F9UT o{w"I0

  MYSQL_ASSOC: 字段名表示键,字段内容为值51Testing软件测试网(B?J~q'aVupr8S%Y

  MYSQL_NUM: 数值索引数组,操作与mysql_fetch_ros()函数一样U,?96;p0

  MYSQL_BOTH: 即作为关联数组又作为数值索引数组返回。result_type的默认值。51Testing软件测试网4R5^CS9Xe9TB{v Kb

  10、mysql_fetch_assoc()-获取和显示数据51Testing软件测试网-v+iS:apr:qJz'sI

  格式:H

  lz6wT)xX0

  array mysql_fetch_assoc (resource result_set)5DKb4r Y|0

  相当于调用 mysql_fetch_array(resource, MYSQL_ASSOC);51Testing软件测试网&ja/W(o5~1yG

  11、mysql_fetch_object()-获取和显示数据51Testing软件测试网S(KAz1GT/J/OJx

  格式:(L2yVSP0

  object mysql_fetch_object(resource result_set)fzYL

  QG[1n/`0

  例:6]P@;WQ)h5~/X3p({ F0

  $query = "select id, name from MyTable order by name";51Testing软件测试网Sr^&UH7VZN+r

  while ($row = mysql_fetch_object($result)) {51Testing软件测试网0{%O'? K;@0b^x

  $id = $row->id;51Testing软件测试网M+h&Q}Sw^

  $name = $row->name;@b? P+`0

  echo "Name: $name ($id)51Testing软件测试网Z1AO!TRY;`5U(g,s

  ";|O.FB@$k^9CSb0

  }5t3S_;Gu/S2nm$tIV0

  说明:返回一个对象,在操作上与mysql_fetch_array()相同7hGk0dUE~0

  12、mysql_num_rows()-所选择的记录的个数51Testing软件测试网EJY[t R

  格式:!yp Zi:z2XgJ#_ V;i`0

  int mysql_num_rows(resource result_set)51Testing软件测试网W.rG"| Q"o

  例:51Testing软件测试网nc#d"|Z9DI0P:`U%R1]

  query = "select id, name from MyTable where id > 65";51Testing软件测试网[6[!Q4ugH%_]

  {

  $result = mysql_query($query);7T)}!cO,Z'aJ|!~0

  echo "有".mysql_num_rows($result)."条记录的ID大于65";'B]t!~%j0

  说明:只在确定select查询所获取的记录数时才有用。51Testing软件测试网E

  {5qJrz{

  13、mysql_affected_rows()-受Insert,update,delete影响的记录的个数5Gf wL|#? B2a?EP t0

  格式:51Testing软件测试网E!w BWGJ$J

  int mysql_affected_rows([resource link_id])51Testing软件测试网8Z]7P

  K|3jmq(x B

  例:,|2DZ6_)Z0

  $query = "update MyTable set name='CheneyFu' where id>=5";51Testing软件测试网2NhK'Tk

  $result = mysql_query($query);k h^9ox0

  echo "ID大于等于5的名称被更新了的记录数:".mysql_affected_rows();51Testing软件测试网i^7uX3@/J6~

  说明:该函数获取受INSERT,UPDATE或DELETE更新语句影响的行数51Testing软件测试网gzc%YO0VV

  14、mysql_list_dbs()-获取数据库列表信息51Testing软件测试网n2s.Id9Zl

  格式:51Testing软件测试网;z%z's! Ts}

  resource mysql_list_dbs([resource link_id])51Testing软件测试网!rGl*l+O2w

  例:z/}`-U4u0

  mysql_connect("localhost", "username", "password");u3taOw0

  $dbs = mysql_list_dbs();J&VKP$zJ0

  echo "Databases:51Testing软件测试网'Y6E#_$C&b

  ";iQ/Z#e]gY0

  while (list($db) = mysql_fetch_rows($dbs)) {51Testing软件测试网B a3eX

  echo "$db51Testing软件测试网9D`w-x8c*t$Y

  ";Q&h#P"V{"A ~C0

  }51Testing软件测试网2IEy2I9tx:m

  说明:显示所有数据库名称C`^7rhM$W)V0T&?P"F0

  15、mysql_db_name()-获取数据库名Z3M8dj)NH T0Iy4B0

  格式:uQ2sj4z,j!i#c/[0

  string mysql_db_name(resource result_set, integer index)5iN N#XP0B*X2@0

  说明:该函数获取在mysql_list_dbs()所返回result_set中位于指定index索引的数据库名Xx:s6~5C0

  16、mysql_list_tables()-获取数据库表列表51Testing软件测试网0d)D1Z4}8B8YX/t!og

  格式:51Testing软件测试网 u!I.k.nAG%z8U9U

  resource mysql_list_tables(string database [, resource link_id])51Testing软件测试网zymIo)d

  例:Mi/Z:WQ.e!N"a4]-xN0

  mysql_connect("localhost", "username", "password");51Testing软件测试网[C K;s$NX3P^I

  $tables = mysql_list_tables("MyDatabase");51Testing软件测试网

  jl[1n-e

  while (list($table) = mysql_fetch_row($tables)) {51Testing软件测试网A%H)]3B)B gS

  echo "$table51Testing软件测试网u E5NY g;e'q

  p

  ";$SC

  I1z'ky6hw0

  }51Testing软件测试网

  rIj1dve4Xe%V(]c

  说明:该函数获取database中所有表的表名51Testing软件测试网o8kOf,F'H

  17、mysql_tablename()-获取某个数据库表名X-jrt.s0

  格式:51Testing软件测试网$~0c"f0b TN}0T!c

  string mysql_tablename(resource result_set, integer index)51Testing软件测试网:[.]#OX&z.m

  例:51Testing软件测试网!XYu]u!rO"W1r

  mysql_connect("localhost", "username", "password");w @y8eTA6R-_0

  $tables = mysql_list_tables("MyDatabase");9[

  hv3d%X2mnUf@1UL0

  $count = -1;51Testing软件测试网5[K.PF V2_

  while (++$count < mysql_numrows($tables)) {51Testing软件测试网4F,xs#nX;F9`1A

  echo mysql_tablename($tables, $count)."

  g1myK[VtH~0";hTeK0K kHI0

  }^9C8j~~:X&_