0

This is one of my script that I was working on but I just can't figure out what's wrong.

$dbhost = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "str_academy";

$connect = mysqli_connect("$dbhost", "$dbusername", "$dbpassword");

if (!$connect) {
   echo("<div class='container'>");         
   echo("<div class='alert alert-danger'><strong>Warning! </strong>Couldn't create the         specified database</div>");
   echo("</div>");;
exit();
}else {
   echo("<div class='container'>");
   echo("<div class='alert alert-success'><strong>Success! </strong>Database connection established successfully</div>");
   echo("</div>");;
}

if ($connect) {
   $database_create = 'CREATE DATABASE str_academy';
   $database_query = mysqli_query($connect, $database_create);
if (!$database_query) {
echo("<div class='container'>");            
    echo("<div class='alert alert-danger'><strong>Warning! </strong>Couldn't create the specified database</div>");
    echo("</div>");
    exit();         
}else {
    echo("<div class='container'>");
    echo("<div class='alert alert-success'><strong>Success! </strong>Database : str_academy successfully created</div>");
    echo("</div>");
}
}

$database_select = mysqli_select_db($connect, "str_academy");

if (!$database_select) {
   echo "Select a database first";
}else {
   echo "Database Selected";
}
$contact_table_create = "CREATE TABLE contact_information (
contact_id INT NOT NULL AUTO_INCREMENT,
contact_name VARCHAR(255) NOT NULL,
contact_email TEXT NOT NULL,
phonenumber INT(11) REAL NULL,
message TEXT NOT NULL,
PRIMARY KEY(contact_id)
)";
$contact_table_query = mysqli_query($connect, $contact_table_create);

if (!$contact_table_query) {
   echo "Table not created";
}else {
   echo "Table created";
}   

?>

I've created this simple test script. Everything seems to be running perfectly except that it is not creating the table contact_information. Can anyone help me solve this problem?

5
  • 3
    What does mysqli_error() return? Commented Jul 15, 2014 at 17:58
  • @Jocelyn It will complain about some syntax error near near 'REAL :-) But yes, that's the way to get into the debugging of scripts. Commented Jul 15, 2014 at 18:00
  • 1
    When I try this: 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 'REAL NULL, message TEXT NOT NULL, PRIMARY KEY(contact_id) )' at line 5. Going to postulate that this is something to do with your usage of the REAL keyword Commented Jul 15, 2014 at 18:01
  • change phonenumber INT(11) REAL NULL, to phonenumber INT(11) NOT NULL, and see if it works Commented Jul 15, 2014 at 18:05
  • Well yes, it was about that REAL part. Thanks Commented Jul 15, 2014 at 18:05

5 Answers 5

1

The keyword REAL used in this context is throwing a syntax 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 'REAL NULL, message TEXT NOT NULL, PRIMARY KEY(contact_id) )' at line 5

Removing REAL from the query (and, if necessary, replacing it with something else) should fix the problems you're having.

Sign up to request clarification or add additional context in comments.

Comments

1

change phonenumber INT(11) REAL NULL, to phonenumber INT(11) NOT NULL, and see if it works

Comments

0

In phpmyadmin / innoDb REAL becomes double so i think you need to replace REAL for double DEFAULT. Your query will be like:

CREATE TABLE contact_information (
    contact_id INT NOT NULL AUTO_INCREMENT,
    contact_name VARCHAR(255) NOT NULL,
    contact_email TEXT NOT NULL,
    phonenumber double DEFAULT NULL,
    message TEXT NOT NULL,
    PRIMARY KEY(contact_id)
)

1 Comment

As the comments above shown you can also use INT(11) instead of double DEFAULT
0

The error is at the column phonenumber INT(11) REAL NULL. This should be phonenumber INT(11) NOT NULL

Comments

-1

Output the mysqli error message. I suspect the user "root" may not have the CREATE DATABASE privileges.

if (!$database_query) {
    $error = mysqli_error($connect);
    echo("<div class='container'>");            
    echo("<div class='alert alert-danger'>{$error}</div>");
}

1 Comment

Although they have given negative vote, I agree with the part of the privileges. Recommend that this type of response you keep in the 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.