how to check if a table is existed?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kevin7
Forum Commoner
Posts: 96
Joined: Fri May 21, 2004 6:54 am

how to check if a table is existed?

Post by kevin7 »

how we know a table is existed in a database?
i want to make a table that is randomly generated using php...

tq
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

"CREATE TABLE `$randomName`"
fails a query.
kevin7
Forum Commoner
Posts: 96
Joined: Fri May 21, 2004 6:54 am

Post by kevin7 »

hmm... u means if a error message come out... means.. the table is exist?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

yep
kevin7
Forum Commoner
Posts: 96
Joined: Fri May 21, 2004 6:54 am

Post by kevin7 »

how can i capture the error message?
i'm a newbie... :)

tq
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$check = mysql_query("CREATE TABLE `$randomName`") or -1;
if($check === -1)
{//  it existed
}
else
{//  it was created
}
User avatar
mendingo
Forum Commoner
Posts: 28
Joined: Sun May 23, 2004 1:27 pm

Post by mendingo »

Won't that leave you with an unwanted table in MYSQL if the table didn't exist?

Surely a better way is to run a SELECT statement on it and see if it fails:

Code: Select all

$results = mysql_query("SELECT * FROM table");
if($results)
{
   //table exists
}
else
{
  //table doesn't exist
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

read the post:
kevin7 wrote:i want to make a table that is randomly generated using php...
Post Reply