Checking if a database exists
Posted: Thu Feb 02, 2006 7:21 pm
How would you check if a mysql database exists with php?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
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;
}Code: Select all
SHOW DATABASES LIKE 'table'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?