Page 1 of 1

Checking if a database exists

Posted: Thu Feb 02, 2006 7:21 pm
by Luke
How would you check if a mysql database exists with php?

Posted: Thu Feb 02, 2006 7:38 pm
by raghavan20
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;
	}

Posted: Thu Feb 02, 2006 10:14 pm
by feyd

Re: Checking if a database exists

Posted: Thu Feb 02, 2006 10:17 pm
by John Cartwright
The Ninja Space Goat wrote:How would you check if a mysql database exists with php?
with php? Makes much more sense to use MYSQL to do the checking :wink: