Jump to content

Unable to create database and tables


prathameshkakade

Recommended Posts

I am trying to create MySQL database using PHP.

Here is my code.

db-config.php

<html>
<head>
<title>
Database Configuration
</title>
</head>
<body>
<?php
$host = "localhost";
$user = "root";
$pass = "123456789";
$database = "hello";

$mysql = new mysqli($host,$user,$pass);
if($mysql->connect_error)
{
	die ("Could not connect to database");
}
?>

createdb.php

<html>
<head>
<title>
Create tables
</title>
</head>
<body>
<?php
include("\db-config.php");
if(!$a = ($mysql->query("CREATE DATABASE $database")))die("Could not create database");
echo "Successfully created database";
$table = "user";
$td  = "userid MEDIUMINT(10) DEFAULT '0' NOT NULL AUTO_INCREMENT,";
$td .= "username VARCHAR(20) BINARY NOT NULL,";
$td .= "password VARCHAR(20) BINARY NOT NULL,";
$td .= "name VARCHAR(20) BINARY NOT NULL";
$test = $mysql->query("SELECT DATABASE $database");
if(!(mysqli_select_db($mysql,$database)))die("Could not connect to database");
if(!(mysqli_query("CREATE TABLE $table ($td)")))die("Could not create tables");
echo "Successfully created tables";
?>
</body>
</html>

It always results in die message.

Please help.

Link to comment
Share on other sites

In real life you'll want to be displaying a message on screen, maybe a "Sorry, Try Later" message to the user instead of using die()

 

For testing you'll want to see what the error was, so that you can fix it.

 

 

 

Maybe something like this:

 

 

$resultHandle = $databaseHandle->query("CREATE DATABASE $database")

if(resultHandle)

{

//Stuff to do if the query looks like it worked

}

else

{

// Stuff to do if the query failed

print 'Error Description: ' . $databaseHandle->error . '----------- Error Number: ' . $databaseHandle->errno;

}

 

 

 

 

 

Also, for your current error, the most common reason for a CREATE DATABASE query to fail is for the database "user" to not have the permission set to allow it to create new databases.

Link to comment
Share on other sites

In real life you'll want to be displaying a message on screen, maybe a "Sorry, Try Later" message to the user instead of using die()

 

For testing you'll want to see what the error was, so that you can fix it.

 

 

 

Maybe something like this:

 

 

$resultHandle = $databaseHandle->query("CREATE DATABASE $database")

if(resultHandle)

{

//Stuff to do if the query looks like it worked

}

else

{

// Stuff to do if the query failed

print 'Error Description: ' . $databaseHandle->error . '----------- Error Number: ' . $databaseHandle->errno;

}

 

 

 

 

 

Also, for your current error, the most common reason for a CREATE DATABASE query to fail is for the database "user" to not have the permission set to allow it to create new databases.

Mysql user which I am using here is root.So I don't understand why the user does not have permission?

Link to comment
Share on other sites

I tried the other way to create tables.

<html>
<head>
<title>
Database
</title>
</head>
<body>
<?php
require("./db-config.php");
//$a = mysqli_query($mysql,"CREATE DATABASE $database");
//if(!$a)die(mysqli_error($mysql));
//else echo "Successfully created database";
$table = "user";
$function = "CREATE TABLE $table (
'userid' MEDIUMINT(10) DEFAULT '0' NOT NULL AUTO_INCREMENT,
'username' VARCHAR(20) BINARY NOT NULL,
'password' VARCHAR(20) BINARY NOT NULL,
'name' VARCHAR(20) BINARY NOT NULL) CHARACTER SET utf8 COLLATE utf8_general_ci";

$test = mysqli_query($mysql,"SELECT DATABASE $database");
if(!$test)die(mysqli_error($mysql));
else
{
	$ct = mysqli_query($mysql,$function);
	if(!$ct)die(mysqli_error($mysql));
	else
	{
		echo "Successfully created tables $table ($td)<br/>";
	}
}
?>

Now it shows the following error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'hello' at line 1.

 

What is the error here?

Link to comment
Share on other sites

the query that's failing is SELECT DATABASE $database. if you want to select a database using a query statement, it would be USE $database

I tried that.Now it is showing the following error.

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DATABASE hello' at line 1

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.