1

I am following a CodeIgniter tutorial, which tells me to make a database with the following code (using mysql):

 $sql = "create database login;
            CREATE TABLE IF NOT EXISTS `user_login` (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `user_name` varchar(255) NOT NULL,
            `user_email` varchar(255) NOT NULL,
            `user_password` varchar(255) NOT NULL,
            PRIMARY KEY (`id`)
            ) ";

I try to run this, using my own php code, but i keep getting this error, and have no idea how to solve it:

Error creating table: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'CREATE TABLE IF NOT EXISTS `user_login` ( `id` int(11) NOT NULL AUTO_INCREM' at line 2

Why does my query fail at the start of the second query statement?

8
  • You can't execute 2 statements at once with your PHP command. Commented Oct 13, 2016 at 9:45
  • Split it up, execute it as two statements. Or better yet, use the migrations feature of Codeigniter. Commented Oct 13, 2016 at 9:46
  • @tadman So you mean i have to split create database and create table? Commented Oct 13, 2016 at 9:48
  • I'm curious as to what the tutorial is. Is it publicly accessible via a link? Commented Oct 13, 2016 at 9:57
  • @TimBrownlaw Yes it is. formget.com/form-login-codeigniter Commented Oct 13, 2016 at 10:02

1 Answer 1

2
create database login;

and

CREATE TABLE IF NOT EXISTS `user_login` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `user_name` varchar(255) NOT NULL,
    `user_email` varchar(255) NOT NULL,
    `user_password` varchar(255) NOT NULL,
    PRIMARY KEY (`id`)
)

are two commands which are to be executed separately. Have the database created before you try to create the table - either via a SQL client or by executing something like:

$sql1 = 'create database login;';
$sql1->execute();

$sql2 = 'CREATE TABLE IF NOT EXISTS `user_login`...';
$sql2->execute();
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.