SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2005
    Posts
    153
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    check if database and table exist

    Hello,
    I am using php and mysql. I want to check to see if a database exists or not before creating one. Is there a simple way to do this?

  2. #2
    SitePoint Evangelist mad-onion's Avatar
    Join Date
    Aug 2000
    Location
    Land of the long white cloud
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP has a function mysql_list_dbs so using that you can loop through all the databases to see if the one you are checking for exists.

    Using the manual example as a starting point something like this...

    <?
    $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
    $db_list = mysql_list_dbs($link);

    $db_exists = false;
    $db = "name_of_db_to_search_for";

    while ($row = mysql_fetch_object($db_list)) {
    if ( $db == $row->Database ){
    $db_exists = true;
    break;
    }
    }

    if ( $db_exists ) {
    //the database exists
    }

    ?>
    SiteOptions >> Services :: Products :: Contact
    Developers of PHP, C++, Visual Basic, MySQL, and more!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •