How do I check if a table exists?

Hi,
In my php code I want to check if tbl_prs_customer exists in my database.
Please help?!

Hinke
hinkeltjeAsked:
Who is Participating?
 
laurlyConnect With a Mentor Commented:
try this

mysql_connect("localhost", "mysql_user", "mysql_password") or die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

$result = mysql_query("show tables");

while ($row = mysql_fetch_array($result)) {
       if $row[0] = "tbl_prs_customer"
          {
     echo "your table exists"

          }

}

mysql_free_result($result);
0
 
hinkeltjeAuthor Commented:
By the way, it is a mySQL database.
0
 
VGRCommented:
yes

or use a query like (not tested) :
$result=mysql_query("DESCRIBE tablename;") or die("doesn't exist"); // of other action if possible
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
fbordinConnect With a Mentor Commented:
If this is a database and not a table that you want to test on then you should  use the SHOW statement to find out what databases currently exist on the server:

mysql> SHOW DATABASES;
+----------+
| Database |
+----------+
| mysql    |
| test     |
| tmp      |
+----------+

I guess you have to  do something like laurly but with Database instead of tables

0
 
Big_Red_DogCommented:
But remember, you do not need to check if it exists when creating a table by using the special "IF NOT EXISTS" when creating the table.

CREATE TABLE IF NOT EXISTS my_table_name ...
0
 
philcartmellCommented:
This is a function to check to see if a table exists:

function mysql_table_exist($tableName)
{
$query = "SELECT COUNT(*) FROM $tableName";
       $result = mysql_query($query);
$num_rows = @mysql_num_rows($result);
if($num_rows)
{
return TRUE;
}
else
{

return FALSE;
}

}
0
 
philcartmellCommented:
Hi - use the below instead - I've optimised it a little:

function mysql_table_exists($dbLink, $database, $tableName)
{
$tables = array();
$tablesResult = mysql_list_tables($database, $dbLink);
while ($row = mysql_fetch_row($tablesResult)) $tables[] = $row[0];
return(in_array($tableName, $tables));
}
0
 
VGRCommented:
groumph ®

it's as good as using a standard mysql_query() with "SHOW TABLES";
0
 
philcartmellConnect With a Mentor Commented:
i presume mysql_list_tables is essentially a php wrapper around show tables. - may as well use the function thats their - why reinvent the wheel ;)
0
 
VGRConnect With a Mentor Commented:
yes and no
yes, it's that
no, because it becomes ***less*** portable
for instance, Oracle or DB2 "drivers in PHP" (wrappers) may very well not have *_liust_table() function implemented, whereas "SHOW TABLES;" in pure SQL will always work :D
0
 
philcartmellCommented:
true. but in this occasion i seriously doubt he'll be porting to oracle lol
0
 
Big_Red_DogCommented:
Did any of this help you?  If so, please award a grade to close this question.
0
All Courses

From novice to tech pro — start learning today.