Luis Rochinha
Luis Rochinha

Reputation: 45

Error, query failed to create a table

I'm new in php, and I'm follow a tutorial to access a MySql Server. I installed the xampp in my desktop, so my web server is localhost. I have two files, "config.php" responsible for the binding configuration and "opendb.php" responsible for connecting to MySql server.

When I run the file initdb.php in my browser, I receive this message:

Error, query failed: CREATE TABLE students( id INT NOT NULL AUTO INCREMENT, fname VARCHAR(15) NOT NULL, lname VARCHAR(15) NOT NULL, PRIMARY KEY(id) )

and don't know why don't creates the table.

This is my file initdb.php:

<?php
include 'config.php';
include 'opendb.php';

$queryFile = 'students.sql';
$queries = implode('', file($queryFile));

$arr = split(';', $queries);
array_pop($arr);

foreach ($arr as $query) {
    $result = mysql_query($query) or die('Error, query failed: '.$query);
}

include 'closedb.php';
?>

And this is the file students.sql:

DROP TABLE IF EXISTS students;
CREATE TABLE students(
id INT NOT NULL AUTO INCREMENT,
fname VARCHAR(15) NOT NULL,
lname VARCHAR(15) NOT NULL, PRIMARY KEY(id)
);

INSERT INTO students (fname, lname) VALUES('Harry', 'Potter');
INSERT INTO students (fname, lname) VALUES('Ron', 'Wesley');
INSERT INTO students (fname, lname) VALUES('Hermione', 'Granger');
INSERT INTO students (fname, lname) VALUES('Chidori', 'Kaname');

Sorry for my bad english.

Upvotes: 0

Views: 295

Answers (2)

user2989408
user2989408

Reputation: 3137

I think it should be AUTO_INCREMENT, example here

Upvotes: 0

ops
ops

Reputation: 2049

Replace this line:

id INT NOT NULL AUTO INCREMENT,

With

id INT NOT NULL AUTO_INCREMENT,

Upvotes: 1

Related Questions