Checking if a database exists
Moderator: General Moderators
Checking if a database exists
How would you check if a mysql database exists with php?
- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
I wrote this function a few days ago...I am have cut this out of a class...
Code: Select all
/*
* Find whether there a database exists under the name provided
* Input - databaseName: String
* Return: Boolean
*/
function isDatabase($databaseName){
$valid = FALSE;
if (empty($databaseName) === FALSE){
$query = "show databases";
if (is_resource($result = mysql_query($query)) === TRUE){
if (mysql_num_rows($result) > 0){
while ($row = mysql_fetch_row($result)){
if ($row[0] == $databaseName){
$valid = TRUE;
}
}
}
}
}
//DEBUG//echo "<br />DEBUG: isDatabase() - is database valid: ".$valid;
return $valid;
}- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
SHOW DATABASES LIKE 'table'- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: Checking if a database exists
with php? Makes much more sense to use MYSQL to do the checkingThe Ninja Space Goat wrote:How would you check if a mysql database exists with php?