Check if a database exist (MySQL) and if not create it in PHP

后端 未结 3 1052
生来不讨喜
生来不讨喜 2021-01-04 11:12

So i want to write a php script who checks in the data base (in localhost, user=\"root\", pass=\"\") \"data1\" exists, and if is not, create it. Please thanks for any help y

相关标签:
3条回答
  • 2021-01-04 11:40
    CREATE DATABASE IF NOT EXISTS DBName;
    
    0 讨论(0)
  • 2021-01-04 11:46

    Check the return value of mysql_select_db - this function will return true when the database exists and can be selected - i.e., the database might exist but the current user may not have permission to access the database. This may be enough to determine in PHP if the database exists - as long as you can guarantee that the PHP MySQL database user will always have access to this database when it exists.

    mysql_connect('localhost', 'root', '');
    if (!mysql_select_db('mydb')) {
        echo("creating database!\n");
        mysql_query('CREATE DATABASE mydb');
        mysql_select_db('mydb');
    }
    
    0 讨论(0)
  • 2021-01-04 11:55

    Send the following to mysql from your php code :
    CREATE DATABASE IF NOT EXISTS YourDB;
    Documentation : http://dev.mysql.com/doc/refman/5.0/en/create-database.html

    0 讨论(0)
提交回复
热议问题