19

I want to create a database. Why is not the db created with this code?

$dbname = 'regulations_db';
    $con = mysql_connect("localhost","root","pass");
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }
if (mysql_num_rows(mysql_query("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '". $dbname ."'"))) {
        echo "Database $dbname already exists.";
    }
    else {
        mysql_query("CREATE DATABASE '". $dbname ."'",$con);
        echo "Database $dbname created.";
    }

This is working, but I think the first one is the best practice:

if (mysql_query("CREATE DATABASE IF NOT EXISTS regulations_db",$con))
    {
        echo "Database created";
    }
    else
    {
        echo "Error creating database: " . mysql_error();
    }

3 Answers 3

26

Just do a simple mysql_select_db() and if the result is false then proceed with the creation.

As an example, check out the first answer here by another very smart StackOverflower.

<?php
// Connect to MySQL
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

// Make my_db the current database
$db_selected = mysql_select_db('my_db', $link);

if (!$db_selected) {
  // If we couldn't, then it either doesn't exist, or we can't see it.
  $sql = 'CREATE DATABASE my_db';

  if (mysql_query($sql, $link)) {
      echo "Database my_db created successfully\n";
  } else {
      echo 'Error creating database: ' . mysql_error() . "\n";
  }
}

mysql_close($link);
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Update: now should use mysqli_select_db, right? -- also would it make sense to repeat "$db_selected = mysqli_select_db('my_db', $link);" in the success condition (i.e. now it should work) -- or did you only use it to check for existence of the database and have therefor no further need for it anyway?
mysql_select_db --> is not recognized for me.
5

Three steps to fix this:

  1. Don’t specify the database name when connecting.
  2. Your SQL statement should be CREATE DATABASE IF NOT EXISTS php1.
  3. Call mysqli_select_db($link, 'php1') to make that the default database for your connection.

1 Comment

Isn't that what the second code that he describes effectively does?
5

If you're using MySQLi Object-oriented method, you can use following code, this code is similar to previous answer and only the method is different, I just put this because if anyone using MySQLi Object-oriented method, you can use this code directly.

$servername = "localhost";
$username = "mysql_user";
$password = "user_password";
$dbName = "databaseName";

// Connect to MySQL
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// If database is not exist create one
if (!mysqli_select_db($conn,$dbName)){
    $sql = "CREATE DATABASE ".$dbName;
    if ($conn->query($sql) === TRUE) {
        echo "Database created successfully";
    }else {
        echo "Error creating database: " . $conn->error;
    }
} 

Furthermore you can refer W3school site here.

Good Luck! :D

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.