6

I am trying to create a database using mysqli if the database name doesn't exist using the following code:

<?php

$link = mysqli_connect("localhost", "root", "", "php1") ; 
if(mysqli_connect_errno()){
    echo mysqli_connect_error();
    $query = "CREATE DATABASE php1";
    if(mysqli_query($link, $query)){
        echo "Success";
    } else {
        echo "Failure";
    }
}


?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>        
        <a href="signup.php"><button type="button">Sign Up!</button></a>
        <a href="login.php"><button type="button">Log In</button></a>
    </body>
</html>

The code checks the db & it doesn't exists. Next I want to create the database if it does not exist but I am getting the following error always:

Warning: mysqli_connect(): (HY000/1049): Unknown database 'php1' in E:\xampp\htdocs\PhpProject1\index.php on line 3

Unknown database 'php1'

Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in E:\xampp\htdocs\PhpProject1\index.php on line 7

Failure

This way I can't get the new DB created. Does anybody has any idea how to correct this?

3 Answers 3

11

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.
Sign up to request clarification or add additional context in comments.

2 Comments

Gee, Thanks! I was stuck here since a long time :) The first point was the most important one i think.
$mysqli = new mysqli($DB_SERVER, $DB_USERNAME, $DB_PASSWORD, "", $DB_PORT); ... $mysqli->query('use ' . $DB_NAME . ';'); worked! Thank you!
1
// Enable error reporting for mysqli
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

// Connection to MySQL server without database name.
$connect = new mysqli('localhost', 'my_user', 'my_password');

// Set the desired charset after establishing a connection
$connect->set_charset('utf8mb4');

// Create database if not exists.
$databaseName = 'database_name';
$query = 'CREATE DATABASE IF NOT EXISTS ' . $databaseName;

// Use the new database for all future SQL queries.
mysqli_select_db($connect, $databaseName);

More information about mysqli_connect : https://www.php.net/manual/en/mysqli.construct.php

1 Comment

Note it is not a good idea to make this code run constantly. It should be separate script that is run only once
-2
$connection = '';
$connection = new mysqli("db", "root", "example");
$query = mysqli_query($connection, "SHOW DATABASES LIKE 'company'");
$result = mysqli_num_rows($query);

if($result <=0){
$create = mysqli_query($connection, "CREATE DATABASE IF NOT EXISTS company;");
echo "Database created successifully";
}

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.