Jump to content

Check If Database Exists


dzhax
 Share

Recommended Posts

I got my previous problem fixed (creating databases)Now i need to be able to check if a database already exists.Anyone know how i would do this?something likeif database exists then create the databaseelse say it existsendthen possibly give option to drop the database and create new oneThanks

Link to comment
Share on other sites

By using mysql_select_db() or the corresponding database extension function, you can check if a database exists, and select it if it does. If it doesn't, you may run a query to create it. If it does, you may remove it with mysql_drop_db() or a corresponding database extension function.

Link to comment
Share on other sites

Ok the checking to see if it exists worked like a charm...The drop database not so much

case dropdb:			$conn = mysql_connect($dbip, $user, $pass);			if (mysql_drop_db($dbname,$conn)){				if ($step == '3'){					$step = '3';				} else {					$step = '2';}				$dbcreated = 'Database could not be deleted';				header('location: index.php?step=2&dbip=' . $dbip . '&user=' . $user . '&pass=' . $pass . '&status=Able To Connect&dbname=' . $dbname . '&dbcreated=' . $dbcreated);				mysql_close($conn);			} else {				if ($step == '3'){					$step = '3';				} else {					$step = '2';}				$dbcreated = 'Database deleted successfully';				header('location: index.php?step=2&dbip=' . $dbip . '&user=' . $user . '&pass=' . $pass . '&status=Able To Connect&dbname=' . $dbname . '&dbcreated=' . $dbcreated);				mysql_close($conn);			}		break;

Does not drop the table or display the $dbcreated when it redirects.this is the checking code. it works but the above dosent... I copied and pasted it and changed the function so it should work.

case existsdb:			$conn = mysql_connect($dbip, $user, $pass);			if (mysql_select_db($dbname,$conn)){				if ($step == '3'){					$step = '3';				} else {					$step = '2';}				$dbcreated = 'Database is not available';				header('location: index.php?step=2&dbip=' . $dbip . '&user=' . $user . '&pass=' . $pass . '&status=Able To Connect&dbname=' . $dbname . '&dbcreated=' . $dbcreated);				mysql_close($conn);			} else {				if ($step == '3'){					$step = '3';				} else {					$step = '2';}				$dbcreated = 'Database is available';				header('location: index.php?step=2&dbip=' . $dbip . '&user=' . $user . '&pass=' . $pass . '&status=Able To Connect&dbname=' . $dbname . '&dbcreated=' . $dbcreated);				mysql_close($conn);			}		break;

Edited by dzhax
Link to comment
Share on other sites

Many hosts don't give their users CREATE/DROP DATABASE privileges, preferring you to go through the user control panel.

Link to comment
Share on other sites

its not gonna be hosted by me. its like when you download a forum and u use install.php to install the database for you. thats what im doing. But the ppl that will be using this might mess up there phpmyadmin so i figured do everything here with detailed instructions.Any help why dropping wont work?

Link to comment
Share on other sites

its not gonna be hosted by me. its like when you download a forum and u use install.php to install the database for you. thats what im doing. But the ppl that will be using this might mess up there phpmyadmin so i figured do everything here with detailed instructions.Any help why dropping wont work?
Many systems ask their users to create a database, and then point to it at installation time. After they point to the DB, the installer adjusts the DB settings if needed, asks the user if they want to override all tables within, etc.You've gone the extra step of trying to create the DB if it doesn't exist, and that's great too. But don't assume you'll have this right, or that you'll have the right to remove it either.The best systems I think don't rely on DB defaults, but instead specify them on each table, and upon installation time have an optional "table prefix" that you can specify in case you want to embed several applications in a single DB (as is the case in many shared hosts that give their users only one DB). Edited by boen_robot
Link to comment
Share on other sites

I understand all of this but I just dont under stand why the drop table will not work...I have full rights to everything cuz i can create the db no problem and check to see if it exists along with the fact it is my system that runs all of the website stuff... But the dropping stops working at some point in the code.The $dbcreated is displayed on the page saying what has happened.It does not display when the page redirects and the table does not drop either.I understand that some might not be able to create the table them selves so I will add a skip this step in the installBut for ppl who can do this i would like to have it available.

Edited by dzhax
Link to comment
Share on other sites

I think the problem may be in your condition:

if (mysql_drop_db($dbname,$conn)){

You say 'Database could not be deleted' when it was infact deleted and vise versa. Either invert your condition bodies, or invert the condition, like:

if (!mysql_drop_db($dbname,$conn)){

BTW, I'm just being picky, but unless existsdb is a constant, consider changing lines like

case existsdb:

to

case 'existsdb':

PHP recovers from that, but with a notice... it's nice when codes don't produce any warnings or notices... and when they shouldn't either.

Edited by boen_robot
Link to comment
Share on other sites

I think the problem may be in your condition:
if (mysql_drop_db($dbname,$conn)){

You say 'Database could not be deleted' when it was infact deleted and vise versa. Either invert your condition bodies, or invert the condition, like:

if (!mysql_drop_db($dbname,$conn)){

BTW, I'm just being picky, but unless existsdb is a constant, consider changing lines like

case existsdb:

to

case 'existsdb':

PHP recovers from that, but with a notice... it's nice when codes don't produce any warnings or notices... and when they shouldn't either.

Ok i will do that but it still doesnt explain why the message is not being sent.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...