Personal Loans - Indian television channel news - Mortgage - Personal Loans - Facebook proxy list

PDA

View Full Version : PHP: If statement for checking if database already exist then...


Fratyr
Feb 18th 2008, 12:16 am
I want to check if the database already exist before creating it, if yes -> say it exist, if no -> create a new one, if error occured -> echo it.

This is what i got for now, what should i add to complete the statement?


if (mysql_query("CREATE DATABASE ".$dbname,$dbcnx));
{
echo "Database created: " . $dbname;
}
else
{
echo "Error creating database: " . die (mysql_error($dbcnx));
}

?>

00johnny
Feb 18th 2008, 1:34 am
i think you can do conditional statements in mysql
ex
create if not exist SOME_TABLE

I don't think that is the exact sytax, but google for the create sytax and check it out on mysqls documentation site.

Fratyr
Feb 18th 2008, 10:30 am
Im looking for PHP solution, not an MySQL code.

Thanks anyway.

NathanH
Feb 18th 2008, 8:09 pm
Pretty simple method:


$result = mysql_query(" SHOW TABLES LIKE '".$dbname."' ");
if( mysql_num_rows($result) ) {
echo "Table exists";
} else {
echo "Table does not exist";
}