1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP: If statement for checking if database already exist then...

Discussion in 'PHP' started by Fratyr, Feb 17, 2008.

  1. #1
    I want to check if the database already exist before creating it, if yes -> say it exist, if no -> create a new one, if error occured -> echo it.

    This is what i got for now, what should i add to complete the statement?

    
    if (mysql_query("CREATE DATABASE ".$dbname,$dbcnx));
    	{
    	echo "Database created: " . $dbname;
    	}
    	else 
    	{
    	echo "Error creating database: " . die (mysql_error($dbcnx));
    	}
    
    ?>
    
    PHP:

     
    Fratyr, Feb 17, 2008 IP
  2. 00johnny

    00johnny Peon

    Messages:
    149
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i think you can do conditional statements in mysql
    ex
    create if not exist SOME_TABLE
    Code (markup):
    I don't think that is the exact sytax, but google for the create sytax and check it out on mysqls documentation site.
     
    00johnny, Feb 18, 2008 IP
  3. Fratyr

    Fratyr Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Im looking for PHP solution, not an MySQL code.

    Thanks anyway.
     
    Fratyr, Feb 18, 2008 IP
  4. NathanH

    NathanH Peon

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Pretty simple method:

    
    $result = mysql_query(" SHOW TABLES LIKE '".$dbname."' ");
    if( mysql_num_rows($result) ) {
      echo "Table exists";
    } else {
      echo "Table does not exist";
    }
    
    PHP:
     
    NathanH, Feb 18, 2008 IP