Home > Blockchain >  MySQL need to reload twice to create table inside the database with php
MySQL need to reload twice to create table inside the database with php

Time:12-02

Currently I am facing a problem which the table cannot be created together with database at the same time. Below is the code I am using.

<?php
    DEFINE ('DB_USER', 'root');
    DEFINE ('DB_PASSWORD', '');
    DEFINE ('DB_HOST', 'localhost');
    DEFINE ('DB_NAME', 'enquiry');

    $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);

    // Make my_db the current database
    $db_selected = mysqli_select_db($dbc, 'enquiry');

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

        if (mysqli_query($dbc, $sql)) {
            echo "Database enquiry created successfully";
        } else {
            echo 'Error creating database: ' . mysqli_error($dbc);
        }
    }

    if ($dbc){
        echo "Connected to database";
    } else {
        echo "Could not connect to database";
    }

    $create_table = "CREATE TABLE enquiry (
                                            id INT PRIMARY KEY AUTO_INCREMENT,
                                            first_name VARCHAR(20),
                                            last_name VARCHAR(35),
                                            email VARCHAR(255) NOT NULL,
                                            phone INT,
                                            street_address TEXT,
                                            city VARCHAR(25),
                                            state VARCHAR(25),
                                            postcode INT,
                                            subject VARCHAR(255),
                                            service VARCHAR(20),
                                            comment TEXT
                                        )";

    
    if (mysqli_query($dbc, $create_table)) {
        echo "Table Enquiry created successfully";
    } else {
        echo "Error creating table: " . mysqli_error($dbc);
    }

    mysqli_close($dbc);
?>

Basically, this code will create a database if the specific database does not exist. Then, I need to create a table because I will use the database immediately. However, I don't understand why the I need to reload the webpage twice so that the program will create the table for me. I have tried loops and sleep() function but nothing works. Is there any way to create database and table at the same time?

This is the message I get when I load once.

Database enquiry created successfully 
Connected to database 
Error creating table: No database selected

This is the message I get when I reload.

Connected to database
Table Enquiry created successfully

So ya, I have no idea how to fix this.

CodePudding user response:

Try something like:

// ...

    $db_selected = mysqli_select_db($dbc, 'enquiry');

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

        if (mysqli_query($dbc, $sql)) {
            echo "Database enquiry created successfully";
            $db_selected = mysqli_select_db($dbc, 'enquiry');
        } else {
            echo 'Error creating database: ' . mysqli_error($dbc);
        }
    }

// ...

You were selecting before table existed, and selecting twice should fix, I mean above I added another select right after DB create query.

  • Related