• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
The Electric Toolbox Blog

The Electric Toolbox Blog

Linux, Apache, Nginx, MySQL, Javascript and PHP articles

  • Applications
  • FCKEditor
  • Apache
  • Windows
  • Contact Us
Home / Listing MySQL tables with PHP

Listing MySQL tables with PHP

This post shows how to use the MySQL "SHOW TABLES FROM" SQL query to get a list of tables using PHP. This list could either be stored to an array or echoed out to web browser, command line etc.

The example code below uses the raw mysql_* functions but you easily enough use a database abstraction library to achieve more or less the same thing. $server, $login, $password and $db are variables which store the obvious.

mysql_connect($server, $login, $password);
$res = mysql_query("SHOW TABLES FROM $db");
while($row = mysql_fetch_array($res, MYSQL_NUM)) {
    echo "$row[0]n";
}

To instead put the tables into an array do the following:

mysql_connect($server, $login, $password);
$res = mysql_query("SHOW TABLES FROM $db");
$tables = array();
while($row = mysql_fetch_array($res, MYSQL_NUM)) {
    $tables[] = "$row[0]";
}

Note that the code examples above pass MYSQL_NUM as the second parameter to the mysql_fetch_array() function. This returns a numeric based array which is easier to work with; an associative array with column names will label the column "Tables_in_$db" where $db is the database name and is a little annoying to work with when doing this sort of thing.

Check Out These Related posts:

  1. An alternative to ORDER BY RAND() for MySQL
  2. jQuery: show plain text in a password field and then make it a regular password field on focus
  3. Create a CSV file from MySQL with PHP
  4. PHP script to make a backup copy of a MySQL table

Filed Under: MySql, PHP

Primary Sidebar

Categories

  • Apache
  • Applications
  • Article
  • Case Studies
  • Email Servers
  • FCKEditor
  • HTML And CSS
  • Javascript
  • Linux/Unix/BSD
  • Microsoft SQL Server
  • Miscellaneous Postings
  • MySql
  • Networking
  • Nginx Web Server
  • Offsite Articles
  • OSX
  • PHP
  • Quick Tips
  • RFC – Request for Comments
  • SilverStripe
  • VMWare
  • VPN
  • Windows
  • WordPress

Recent Posts

  • Vim Show Line Numbers
  • Add User To Group Linux
  • Chmod 777 Tutorial
  • How to Copy Directory Linux
  • Linux create user

Copyright © 2021. ElectricToolBox. All Rights Reserved.

  • Contact Us
  • Copyright Info
  • Privacy Policy
  • Sitemap