简体   繁体   中英

SQL Query works in MySQL Admin but not in PHP mysqli

I know that similar questions were asked already here on stackoverflow, but after trying all things that were mentioned I still did not come to a solution.

This is my query:

$query = "CREATE TABLE IF NOT EXISTS `sygauth`.`accounts` (
        `id` INT NOT NULL,
        `email` VARCHAR(64) NOT NULL,
        `pass` VARCHAR(64) NOT NULL,
        `name` VARCHAR(64) NULL,
        `last_name` VARCHAR(64) NULL,
        `display_name` VARCHAR(64) NOT NULL,
        `last_login` DATE NULL,
        `disabled` INT NOT NULL,
        PRIMARY KEY (`id`));
    CREATE TABLE IF NOT EXISTS `sygauth`.`products` (
        `id` INT NOT NULL,
        `name` VARCHAR(64) NOT NULL,
        PRIMARY KEY (`id`));"

I then execute the query via a mysqli connection. When I execute this query via my admin interface (without the double quotes ofc) it works just fine. As soon as I try to execute the query via PHP, I get this 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 'CREATE TABLE IF NOT EXISTS `sygauth`.`products` (   `id` INT NOT NULL,  `name` ' at line 1" 

I just can't figure out where the error lies here :( I hope someone can help me, I bet it's a pretty simple fail on my side, but as i said already, I could not find any solution here yet.

Greets!

If you're using mysqli_query you can't execute more than 1 sql statement at a time (you have 2 create statements in your query). Use mysqli_multi_query instead if you must run both statements in one call. Otherwise call mysqli_query twice.

Since you're trying to execute two statements in a single query, Its somewhat assumed to be the problem. Try executing using two mysqli_query(); like:

$query1 = "CREATE TABLE IF NOT EXISTS `sygauth`.`accounts` (
        `id` INT NOT NULL,
        `email` VARCHAR(64) NOT NULL,
        `pass` VARCHAR(64) NOT NULL,
        `name` VARCHAR(64) NULL,
        `last_name` VARCHAR(64) NULL,
        `display_name` VARCHAR(64) NOT NULL,
        `last_login` DATE NULL,
        `disabled` INT NOT NULL,
        PRIMARY KEY (`id`));"

$query2 = "CREATE TABLE IF NOT EXISTS `sygauth`.`products` (
        `id` INT NOT NULL,
        `name` VARCHAR(64) NOT NULL,
        PRIMARY KEY (`id`));"

mysqli_query($query1);
mysqli_query($query2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM