简体   繁体   English

SQL查询可在MySQL Admin中使用,但不适用于PHP mysqli

[英]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. 我知道在这里已经对stackoverflow提出了类似的问题,但是在尝试了所有提到的事情之后,我仍然没有找到解决方案。

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. 然后,我通过mysqli连接执行查询。 When I execute this query via my admin interface (without the double quotes ofc) it works just fine. 当我通过管理界面(不带双引号ofc)执行此查询时,它就可以正常工作。 As soon as I try to execute the query via PHP, I get this error: 一旦我尝试通过PHP执行查询,就会收到此错误:

"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). 如果使用的是mysqli_query ,则一次最多只能执行1条sql语句(查询中有2条create语句)。 Use mysqli_multi_query instead if you must run both statements in one call. 如果必须在一个调用中同时运行两个语句,请改用mysqli_multi_query Otherwise call mysqli_query twice. 否则,调用mysqli_query两次。

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(); 尝试使用两个mysqli_query();执行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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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