LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-18-2005, 10:25 PM   #1
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Finding What Tables Exist In A MySQL Database


Hello; I have a LAMP server and I'm trying to write a PHP script to help keep track of some information on a database. I want the web interface to present to the user the option of what table to draw queries from. The database will be constant, but the table will be chosen by the user. For example, if we're using database "languages" and tables "c++", "lisp", and "ruby" exist in it, I want to be able to tell the user that they can choose from either "c++", "lisp", or "ruby". In other words, I need to discover from the database what tables exist inside it. Is there a way to do this with a PHP function or a MySQL command? Thanks.
 
Old 12-18-2005, 10:36 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
From the mysql interpreter, you'd do something like:

show databases;
use mydb;
show tables;
describe mytable;

I'm sure you can do the equivalent from PHP...

For example:

http://dev.mysql.com/doc/refman/5.0/en/show.html
Code:
  mysql_select_db("foobar");
  $query = "show tables";
  $result = mysql_query($query);
  $num_results = mysql_num_rows($result);
  print "There are $num_results tables.<br>";
  for ($i = 0; $i < $num_results; $i++)
  {
    $row = mysql_fetch_array($result);
    print "table " . $row[0] . " exists.<br>";
  }
PS:
Nice web site!

Last edited by paulsm4; 12-18-2005 at 10:41 PM.
 
Old 12-19-2005, 10:29 AM   #3
Pauli
Member
 
Registered: Feb 2004
Location: Montreal
Distribution: Gentoo/Debian
Posts: 365

Rep: Reputation: 30
Couldn't you just query it?

Select Table_Name from User_Tables;

EDIT: Sorry if this is wrong, I'm used to Oracle not MySQL.
 
Old 12-19-2005, 10:53 AM   #4
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Original Poster
Rep: Reputation: 43
Ah, the SHOW command; somehow that one slipped past me. Thanks, Paul!
 
Old 12-19-2005, 11:00 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by Pauli
Couldn't you just query it?

Select Table_Name from User_Tables;
You cannot use "Select" to get table names. But on the other hand, "SHOW TABLES" is a (limited) query in MySQL. And you can use "FROM database_name" and "LIKE 'col%'" with "SHOW TABLES".
 
Old 12-19-2005, 11:08 AM   #6
Pauli
Member
 
Registered: Feb 2004
Location: Montreal
Distribution: Gentoo/Debian
Posts: 365

Rep: Reputation: 30
Oh well. It works in oracle
 
Old 12-19-2005, 11:24 AM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
I'll take your word for it
Unfortunately these kind of things are apperently not part of the SQL-standaard.
 
Old 12-19-2005, 09:52 PM   #8
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Original Poster
Rep: Reputation: 43
Oracle

It looks like "SELECT Table_Name FROM User_Tables" may get the metadata regarding what tables are present directly from the database. Since this is part of the idea behind Relational Databases (that they self-contain their metadata in the same manner that they would any other data), that would make perfect sense. I wonder if you couldn't do the same thing in MySQL, assuming you knew what hidden system-table stored metadata about the rest of the tables in that particular database.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Update database tables on different servers securely daveedj Linux - General 0 12-13-2005 12:40 PM
SQLite database security - doesn't exist? vharishankar Linux - Security 2 11-30-2005 08:40 PM
mysql reinstallation distorted by previous tables in /var/mysql mad4linux Linux - Software 0 10-04-2005 01:39 PM
How to compare records in two tables in seperate My Sql database using shell script sumitarun Programming 5 04-14-2005 09:45 AM
mysql count tables in a database Longinus Programming 2 04-11-2004 01:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration