Test If a MySQL Table Exists
This script is called test-if-table-exists.php
First, we include the config.php script to ensure that our connection to our database is made correctly. This file has the codes below in it:
$theemailaddress = "yoursite@yoursite.com"; //EDIT ME
$roothostname = "localhost";
$theusername = "yourusername"; //EDIT ME
$thepassword = "yourpassword"; //EDIT ME
$thedatabasename = "yourdb"; //EDIT ME
mysql_connect("".$roothostname."","".$theusername."","".$thepassword."") or die(mysql_error());
mysql_select_db("".$thedatabasename."") or die(mysql_error());
At the bottom of this page, we have the function mysql_table_exists(). You need not stick these codes in a function, of course. We often use this version of the algorithm to do the table-test work without messing with a function:
$exists = mysql_query("SHOW TABLES LIKE '$a'") or die(mysql_error());
$num_rows = mysql_num_rows($exists);
if($num_rows==0){mysql_close();
echo '<script language="javascript">alert("Table does not exist. Try again");</script>';}
This is very handy when a user enters an incorrect MySQL table name. You can, of course, respond by echoing a message and hoping they see it or—worse yet—echoing the message but then exiting the script. We prefer a nice looking JavaScript alert. It forces a pause and then is easy to continue from.
The reason we are willing to use JavaScript to interface with visitors is because that is what the language was designed to do. JavaScript is the ONLY decent browser language that really and truly will greatly enhance the experience of your site visitors. PHP is by far the best server language—and we love it—but it doesn't do that well at browser interface, since it was not designed for that. All it can do is parse code on the server and send the results to the browser. Whatever gets sent is what you are stuck with—UNLESS YOU RELY ON JAVASCRIPT to really improve the visitors' experiences. Of course, we often use PHP to echo DIVs and FORMs to the screen. Echoing HTML tables and putting MySQL table content into the table cells has become a staple of website coding strategy everywhere. But other times you have to get the PHP data sent to JavaScript (usually with the help of JavaScript Object Notation) or the script will not work. People who write only simple scripts may avoid this, but the rest of us know that JavaScript and its cousin Ajax are essential website creation tools.
Few of our scripts will work without JavaScript. One cannot register, enter data, get from here to there, etc., in most of our scripts without it. What serious web surfer turns off JavaScript? In case you were not aware, many sites rely totally on JavaScript for menu functioning and some of their scripts. And what about data entry? In case you didn't know it, it is a huge convenience for the user because of the way it does input validation. A good site will validate in JavaScript as well as PHP. When the JavaScript data validation script catches unacceptable input, it can simply send focus to the input box where the bad input happened, the user fixes it, and the script is submitted. But if JavaScript is disabled, the user gets sent to PHP data validation which catches the bad data and sends the user back to the input form to redo all input from scratch. The JavaScript data validation script will not make a user restart, if well written. If you have experienced restarting data entry in a long form due to an accidental character, you know exactly what we are talking about. It's maddening! And a good way to get users to surf away from your site forever. If a person turns off THE major browser scripting language just because of a miniscule chance of encountering a scripting exploit on some web page, rather than installing good anti-spyware and anti-virus software, his Internet experience overall will be greatly diminished. Many sites have no alternatives to their script-enabled navigation, so the person is 100% screwed on those sites. But even on those with the alternative, it is always cumbersome and awkward. Point taken?
Anyway, feel free to use this script for table testing. We have yet to see it fail. There are other methods that usually work, but not always, and these often involve such things as SELECT 1 FROM $table LIMIT 1. That was our way at first, until we hit a scenario where it balked. Now our queries for table checking will be
SHOW TABLES LIKE '$table'. There are those that swear by "information_schema.TABLES" but why get obscure? The MySQL link just cited admits that SHOW TABLES LIKE '$table' is a shorter, easier, more concise way of doing table testing than "information_schema.TABLES" methods. Do you really want to do SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = 'db_name' [AND table_name LIKE 'wild']?
The script below is called: test-if-table-exists.php
<?php
include('config.php');
// test-if-table-exists.php
function mysql_table_exists($table){
$exists = mysql_query("SHOW TABLES LIKE '$table'") or die(mysql_error());
$num_rows = mysql_num_rows($exists);
if($num_rows>0){return true;}else{return false;}}
$table="table_name_to_test";
if(mysql_table_exists($table)){echo "it exists";}else{echo "it does not exist";}
mysql_close();
?>