首页 > 开发 > 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. { 
  14.  $tables=array(); 
  15.  $r=fetch_all("show tables"); 
  16.  foreach($r as $v) 
  17.  { 
  18.   foreach($v as $v_) 
  19.   { 
  20.    $tables[]=$v_; 
  21.   } 
  22.  } 
  23.  return $tables; 
  24. } 
  25. function fetch_all($sql) 
  26. { 
  27.  $rs=mysql_query($sql); 
  28.  $result=array(); 
  29.  while($rows=mysql_fetch_array($rs,mysql_assoc)) 
  30.  {//开源代码Vevb.com 
  31.   $result[]=$rows; 
  32.  } 
  33.  
  34.  return $result; 
  35.  
  36. }

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