Page 1 of 1

How to check existence of a mysql table?

Posted: Sat Mar 16, 2013 12:44 pm
by infomamun
Hi there,
I want to check whether a mysql table exists in a database and run some code based on returned result.
I used the following php code:

Code: Select all

mysql_connect("localhost", "[Username]","[Password]") or die(mysql_error());
mysql_select_db([Database_name) or die(mysql_error());
$query = @mysql_query("SHOW TABLES LIKE '$table'");
if(mysql_num_rows($query)==0){
echo 'Table does not exist';
}else{
echo 'Table exist';
}
But it is always returing this without showing 'Table does not exist':
[text]
Table '[Database_name].[table_name]' doesn't exist
[/text]

I used '@' to suppress the error but it is not working.

How to check existence of a table by php code?

Re: How to check existence of a mysql table?

Posted: Sat Mar 16, 2013 1:54 pm
by requinix
There are very, very few circumstances where it's okay to use @. This is not one of them.

That error message isn't coming from the code you posted. Where is it coming from?

Re: How to check existence of a mysql table?

Posted: Sat Mar 23, 2013 12:19 pm
by infomamun
Sorry requinix,
For late reply. Yes the error is coming from another chunk of code withing same script. I solved it already. Thanks for your help.
Best Regards