首页 > 开发 > PHP > 正文

php mysql获取指定数据库所有表名

2024-05-04 21:59:04
字体:
来源:转载
供稿:网友

如果要显示mysql一个指定数据库的表名的话方法很简单,mysql提供了一个show tables命令,它返回的是一个数据,下面来看我做的详细实例,经过测试完全可用,代码如下:

  1. $cn = mysql_connect('localhost','root','root'); 
  2. mysql_select_db('test',$cn); 
  3. print_r(get_tables()); 
  4. /*输出结果 
  5. array 
  6. ( 
  7.     [0] => abc 
  8.     [1] => cn_user 
  9.     [2] => test1 
  10. ) 
  11. */ 
  12. function get_tables() //获取所有表表名 
  13.  $tables=array(); 
  14.  $r=fetch_all("show tables"); 
  15.  foreach($r as $v
  16.  { 
  17.   foreach($v as $v_
  18.   { 
  19.    $tables[]=$v_
  20.   } 
  21.  } 
  22.  return $tables
  23. function fetch_all($sql
  24.  $rs=mysql_query($sql); 
  25.  $result=array(); 
  26.  while($rows=mysql_fetch_array($rs,mysql_assoc)) 
  27.  {//开源代码Vevb.com 
  28.   $result[]=$rows
  29.  } 
  30.  
  31.  return $result
  32.  
  33. }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表