phpmysqlcodeignitermulti-query

Attempting to execute two SQL queries with one query() call in CodeIgniter fails before the start of the second statement


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?


Solution

  • 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();