MySQL Forums :: Data Dictionary & Information Schema :: How to find if a table exists in a specific database in PHP or MySQL


Advanced Search

How to find if a table exists in a specific database in PHP or MySQL
Posted by: Aaron Winborn ()
Date: April 06, 2007 02:54PM

In Drupal (an open source CMS), there's a function to determine if a table exists. Basically, it calls:

SHOW TABLES LIKE 'tablename';

and returns the number of rows.

However, Drupal also allows one to 'prefix' tables in the database. Traditionally, this is used so you can host multiple Drupal installations using one database. Thus, the query used would be:

SHOW TABLES LIKE 'PREFIX_tablename';

A common usage of the prefixes is to share user tables across installations, or even across databases. In this case, the prefix might be set as DBNAME.PREFIX_, so that queries like the following will work:

SELECT * FROM DBNAME.PREFIX_users;

Unfortunately, the first query fails in that case, because the php spits out a query like:

SHOW TABLES LIKE 'DBNAME.PREFIX_tablename';

So that means that Drupal either needs to disallow prefixes of the format DBNAME.PREFIX_, or find another solution to discover the existence of a table, assuming the tablename might include DBNAME.

Any ideas? I've also posted an issue at Drupal related to this at http://drupal.org/node/134308

Options: ReplyQuote


Subject Views Written By Posted
How to find if a table exists in a specific database in PHP or MySQL 31160 Aaron Winborn 04/06/2007 02:54PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.