php源码分享 - 获取mysql数据库中所有表

时间:2016-07-06
php获取某一数据库中的所有表名称可以使用mysql_list_tables函数,本文章向大家分享php获取mysql数据库中所有表的源码实例,需要的朋友可以参考一下本文章的PHP代码

源代码如下:

<?php

   mysql_connect("127.0.0.1","root","password");

   $tables = mysql_list_tables("information_schema");

   while (list($table) = mysql_fetch_row($tables)) {
      echo "$table <br />";
   }

?>

但由于mysql_fetch_row函数已经过时,因此运行上面代码会出现以下错误:

Deprecated: Function mysql_list_tables() is deprecated 

我们可以使用mysql_query("SHOW TABLES FROM $database"); 代替mysql_fetch_row函数,因此正确是源代码如下:

<?php

   mysql_connect("127.0.0.1","root","password");

   $tables = mysql_query("SHOW TABLES FROM information_schema"); 

   while (list($table) = mysql_fetch_row($tables)) {
      echo "$table <br />";
   }

?>

本示例获取本地数据库服务器上information_schema数据库的所有表名称,