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
Checking for existence of MySQL table
Moderator: General Moderators
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.
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.
- harrisonad
- Forum Contributor
- Posts: 288
- Joined: Fri Oct 15, 2004 4:58 am
- Location: Philippines
- Contact:
re
why not query the table then check if an error occur:
just make sure your query is correct.
Code: Select all
$tablename = "sometable";
query = "select * from $tablename";
if(mysql_query($query))
echo "table exists";
else
echo "table doesn't exists";...
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
)
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