Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Mar 2009
    Posts
    120
    Thanks
    13
    Thanked 3 Times in 3 Posts

    code that returns true if a database exists...

    sorry, i made a mistake when naming the topic name, its:
    code that returns true IF a database exists...

    i've been trying to discover what that code is, but with no sucess, something like:

    PHP Code:
    if(thereis_database($dbname)) { echo $dbname.' exists!'; } 

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    You could use mysql_select_db() which returns true/false, though you may need to select your desired DB after this query, to proceed with rest of your queries.

    Let me ask, what's your ultimate aim? Mysql's create databse command has as option IF NOT EXISTS. Are you looking for that?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Regular Coder
    Join Date
    Mar 2009
    Posts
    120
    Thanks
    13
    Thanked 3 Times in 3 Posts
    the reason why i'm doing this is, when the install.php file is opened, the code checks if there is a database with the same name before creating one. if there is one, the code will ask if you're willing to delete it before creating a new one. that's it.

    by the way, thanks.

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    You could also execute the query:
    Code:
    show databases;
    And look through the output to see if there is a DB with the name you want to use.
    OracleGuy

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Dhuan View Post
    the reason why i'm doing this is, when the install.php file is opened, the code checks if there is a database with the same name before creating one. if there is one, the code will ask if you're willing to delete it before creating a new one. that's it.

    by the way, thanks.
    Code:
    create database if not exists yourdbname
    mysql_num_rows() will return 1 row if yourdbname not exists, 0 if exists.
    then if return 0 row you can use
    Code:
    drop database yourdbname
    to remove yourdbname.

    best regards


  •  

    Posting Permissions

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