By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,192 Members | 1,714 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,192 IT Pros & Developers. It's quick & easy.

Check if table is exist or not

100+
P: 190
May this question exist....But i confused...I want to check whether the table is exist or not ....


Like i run a project , on that project i create a temp tables while run time.... on that i want to chech a table is exist or not...

if employee table is exist

exit;
else
create table employee
Sep 6 '08 #1
Share this Question
Share on Google+
3 Replies


10K+
P: 13,264
Check the refmanual for the create table syntax and read about the IF NOT EXISTS clause.
Sep 8 '08 #2

100+
P: 190
tnx,

but this one will gove more easiest way (i hope)...
Expand|Select|Wrap|Line Numbers
  1. if(!(mysql_query("select * from table")))
  2. {
  3. echo "table not found";
  4. }
  5. else
  6. {
  7. echo "table found";
  8. }
  9.  
Sep 8 '08 #3

Atli
Expert 5K+
P: 5,058
That is an extremely wasteful method. You would in fact be fetching ALL the data from that table, just to see if it exists.

A far better way would be to use the SHOW TABLES command, like:
Expand|Select|Wrap|Line Numbers
  1. SHOW TABLES LIKE 'myTableName';
  2.  
Which would return an empty set if the table doesn't exists, or a single row if it does.

P.S.
Use [code] tags when posting code examples.
Please read the Posting Guidelines before posting.

Thank you.
MODERATOR
Sep 8 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.