PDA

For continued disscussion on this topic : (mysql) dropping databases



jewishj
06-23-2001, 07:36 PM
how can i check if a database exists mysql and if it does drop it?

jonsteele
06-23-2001, 07:51 PM
You can detect if a database exists like this:


<?
$link = mysql_connect("localhost","root");

if(@mysql_select_db($dbname,$link)){
echo "Exists!";
}else{
echo "Doesn't exist!";
}

?>

To drop a db, check out http://www.php.net/manual/en/function.mysql-drop-db.php.

mpjbrennan
06-29-2001, 03:21 PM
Have you tried installing phpMyAdmin - this will let you see all the databases on whichever server you connect to (assuming you have access rights) and allow you to do a whole lot of management functions.

patrick

PS - it's free!

jewishj
06-29-2001, 08:10 PM
hey mp:

the reason why i cant just use the winmysql admin panel is the fact that i needed this for an install script for a program that im making, so that it will perform a 'clean' install. I other words the install script creates a db for use with the program, and I want to check if the db already exists, before I attempt to create it. If it does i want it to be dropped so if the buyer of the program wants to start over, they dont have to drop the db manually.....

btw: jon...

do you see any probs with this code... Its simpler than yours so I wanted to see if you saw any situations where it may just cause an error..




if(mysql_select_db('asrdb')){
mysql_drop_db('asrdb');
}
mysql_create_db('asrdb');
or die("Cannot create database");

etc.

jonsteele
06-29-2001, 08:17 PM
I think it's basically the same.

The trick is to use the mysql command's true/false return value in an if statement.

Have you tried your code in a situation where the db did NOT exist and didn't need to be dropped? The '@' stops any errors from interrupting your script. But if your code works in a situation where a db doesn't need to be dropped, then it's unnecessary.

Jon.