How to Check if a MySQL Table Exists Using PHP



PHP







In this article, we show how to check if a MySQL table exists using PHP.

Using the code below, you can check to see if a MySQL table exists.




So once we make the connection to the database, we can then check to see if the table exists.

We create a variable named $exists and set it equal to mysql_query where we search 1 from the table name.

In this case, we are looking for a table name named customers to see whether it exists.

The $exists variable stores the boolean value of true or false.

If it is true (not equal to false), then the table exists. If it is equal to false, the table doesn't exist.

This is a very quick to way to check.

If you want have the have the table name stored as a variable, the only thing that changes is instead of putting the name of the table name directly into the mysql_query, you would put the variable name in its place. You then of course have to have the variable initalized somewhere above in the code.

The following link gives an example of this: Code to Check if a MySQL Table Exists Using a Variable for the Table Name.

Actual PHP Output

One of the tables that I have listed on my database is Files. So if I run this code with the table set to Files, I get the following code shown below.