PHP MySQL get all the table names for the specified database

Source: Internet
Author: User
Tags foreach php mysql table name

If you want to display MySQL a specified database table name the method is very simple, MySQL provides a show tables command, it returns a data, the following I do a detailed example, tested fully available
*/

The code is as follows Copy Code

$CN = Mysql_ Connect (' localhost ', ' root ', ' root ');
mysql_select_db (' Test ', $CN);
Print_r (Get_tables ());

/* Output results

Array
(
    [0] => ABC
    [1] => cn_user
    [2] =& Gt Test1
)
*/

Function get_tables ()//Get all table table names
{
  $tables =array ();
  $r =fetch_all (" Show tables ");
 foreach ($r as $v)
 {
  foreach ($v as $v _)
  {
   $ Tables[]= $v _;
  }
 }
 return $tables;
}

Function Fetch_all ($sql)
{
  $rs =mysql_query ($sql);
  $result =array ();
  while ($rows =mysql_fetch_array ($rs, MYSQL_ASSOC))
 {
   $result []= $rows;
 }
&NBSP
 return $result;
 
}


//Site Original tutorial reproduced annotated from http://www.111cn.net reserved connection address, otherwise will be! 
 ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.