Skip to content


MySQL - Does Table Exist w/o Throwing Errors

There are times where you would like to know if a table exists before executing an query. Most solutions require having MySQL throw an error saying "table does not exist," but I prefer a cleaner way. I found on this forum post a clean way to do it:

SQL:
  1. /* example with table name: table_name */
  2. SHOW TABLES LIKE 'table_name';

This solution will return 1 row if it exists, and 0 rows if it doesn't. Here is a PHP example using this:

PHP:
  1. function DoesTableExist($name)
  2. {
  3.    $sql = "SHOW TABLES LIKE '$name'";
  4.    $result = mysql_query($sql);
  5.    if(mysql_num_rows($result)> 0)
  6.    {
  7.       return true;
  8.    }
  9.    else
  10.    {
  11.       return false;
  12.    }
  13. }
  14.  
  15. if(DoesTableExist('users'))
  16. {
  17.    echo "Users table found!";
  18. }
  19. else
  20. {
  21.    echo "Users NOT FOUND!!!";
  22. }

Related Posts

  1. MS SQL 2005 (T-SQL) Row Count for Each Table At work I needed a solution to give me a count of how many rows each table contained. I've always liked phpMyAdmin's ability to list all the tables and show their size and row count. I've found it immensely helpful. However, I couldn't find anything similar for SQL Server Manager...
  2. MySQL & PHP - SQL_CALC_FOUND_ROWS - An easy way to get the total number of rows regardless of LIMIT Here is a little trick I found awhile back. I faced a challenge several months ago where I had to query a large, complex data result from MySQL. I "paged" through the results using LIMIT and OFFSET. However, I wanted to know the total number of rows w/o the LIMIT....
  3. ASP .NET, LINQ, GridViews, and GUID Errors I found a simple problem today. Here is the deal, there were several times where I had to base a LinqDataSource off a Guid that I would set in the Page_Load(). I created an<asp:HiddenField /> to hold the value so all my DataSources could pull it from the control. The...
  4. PHP Design - Biggest Database Oversights Over the last three years I've had the opportunity to work on several PHP projects, some of them having grown rapidly and required to scale quickly. Three in particular have been a fantastic learning experience for me. Now I don't consider myself a total expert, but I thought I would...
  5. PHP Singletons, Sub-Classing, and HAS-A Relationships I've been very busy these last fews weeks and have neglected making any posts. While there are a variety of subjects I'd love to post about, they'll most likely have to wait until the next year. However, I thought I might be able to throw up a quick example of...

Posted in Programming. Tagged with , , , .

One Response

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Continuing the Discussion

  1. autocarsinsurance.net » Blog Archive » MySQL - Does Table Exist w/o Throwing Errors linked to this post on November 3, 2008

    [...] Most solutions require having MySQL throw an error saying “table does not exist,” but I prefer a cleaner way. I found on this forum post a clean way to do it:. PLAIN TEXT. Read more [...]

Some HTML is OK

(required)

(required, but never shared)

or, reply to this post via trackback.

Powered by WP Hashcash