Shishir Kumar Singh Shishir Kumar Singh - 15 days ago 6
MySQL Question

Table cannot be created

I have written this code, but still table can not be created in database. And just output is:


"database created.... Sorry Table Not Created".


<?php
include 'connection.php';
//Creating database
$creatingdatabase = "CREATE DATABASE IF NOT EXISTS quest";
$query1 = mysqli_query($con, $creatingdatabase);
if($query1){

echo "database created....";
//creating tables
$creatingtable = "CREATE TABLE quiz (
id INT(6) AUTO_INCREMENT PRIMARY KEY,
questions VARCHAR(255) NOT NULL,
optiona VARCHAR(255) NOT NULL,
optionb VARCHAR(255) NOT NULL,
optionc VARCHAR(255) NOT NULL,
optiond VARCHAR(255) NOT NULL,
answer VARCHAR(50) NOT NULL
)";
$query2 = mysqli_query($con, $creatingtable);
if($query2){echo "table created...";}
else { echo "sorry... table not created"; }

}
else { echo "database can not be created"; }
?>


This is the output as seen in the browser.

Answer

The CREATE TABLE statement needs to specify the database to use, or you need to set the default database with mysqli_select_db().

$creatingdatabase = "CREATE DATABASE IF NOT EXISTS quest";
$query1 = mysqli_query($con, $creatingdatabase);
mysqli_select_db($con, "quest");
Comments