Page 1 of 1
Checking for existence of MySQL table
Posted: Tue May 03, 2005 7:13 am
by mjseaden
Hi,
Can you tell me if any PHP functions or MySQL codes allow to check for the existence of a given table. I've looked up my SQL reference and can't seem to see any function that facilitates this.
Many thanks
Mark
Posted: Tue May 03, 2005 7:40 am
by phpScott
not sure how to use the results but there are two ways I can think off.
1)do a simple select and check the error code for the message 'table tablename doesn't exists' type thing or check for error number 1146 I think.
or
2) use 'SHOW TABLES' and loop through the results to see if the table your looking for exists.
Posted: Tue May 03, 2005 11:00 am
by Skara
don't know if this is what I think it is or not, but there's "IF NOT EXISTS"
re
Posted: Wed May 04, 2005 12:11 am
by harrisonad
why not query the table then check if an error occur:
Code: Select all
$tablename = "sometable";
query = "select * from $tablename";
if(mysql_query($query))
echo "table exists";
else
echo "table doesn't exists";
just make sure your query is correct.
...
Posted: Wed May 04, 2005 12:31 am
by Calimero
Found this on php.net - mysql_list_tables() :
http://www.php.net/manual/en/function.m ... tables.php
or maybe mysql_table_name() - check the php.net for exact function name.
My proposition:
List tables, check if yours exist, do whatever with result.
Or do a mysql_query for checking the existence of a table ( in good old sql code - which I don't know how to do it

)