...

View Full Version : code that returns true if a database exists...



Dhuan
04-20-2009, 01:38 AM
sorry, i made a mistake when naming the topic name, its:
code that returns true IF a database exists...

i've been trying to discover what that code is, but with no sucess, something like:


if(thereis_database($dbname)) { echo $dbname.' exists!'; }

abduraooft
04-20-2009, 09:05 AM
You could use mysql_select_db( (http://php.net/mysql_select_db)) which returns true/false, though you may need to select your desired DB after this query, to proceed with rest of your queries.

Let me ask, what's your ultimate aim? Mysql's create databse command has as option IF NOT EXISTS. Are you looking for that?

Dhuan
04-21-2009, 12:05 AM
the reason why i'm doing this is, when the install.php file is opened, the code checks if there is a database with the same name before creating one. if there is one, the code will ask if you're willing to delete it before creating a new one. that's it.

by the way, thanks.

oracleguy
04-21-2009, 08:11 AM
You could also execute the query:

show databases;

And look through the output to see if there is a DB with the name you want to use.

oesxyl
04-21-2009, 10:33 AM
the reason why i'm doing this is, when the install.php file is opened, the code checks if there is a database with the same name before creating one. if there is one, the code will ask if you're willing to delete it before creating a new one. that's it.

by the way, thanks.



create database if not exists yourdbname

mysql_num_rows() will return 1 row if yourdbname not exists, 0 if exists.
then if return 0 row you can use


drop database yourdbname

to remove yourdbname.

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum