duanshaiduhao2471 2017-04-18 19:37
浏览 31
已采纳

SQL查询无法在php中工作但在phpmyadmin中工作[重复]

This question already has an answer here:

I am messing around with OOP mysqli. And I stumbled upon this problem:

There was an error running the query [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 engine.users ( ID INT(11) NOT NULL, USERNAME ' at line 1]

This is the PHP code:

    function InstallDB($db){
    $sql = 
    "CREATE DATABASE IF NOT EXISTS `$this->dbname`; ".
    "CREATE TABLE IF NOT EXISTS `$this->dbname`.`$this->users` ( ".
    "`ID` INT(11) NOT NULL, ".
    "`USERNAME` varchar(60) NOT NULL,".
    "`PASSWORD` varchar(60) NOT NULL,".
    "`EMAIL` varchar(100) NOT NULL,".
    "`IMAGE` varchar(250) NULL,".
    "`LEVEL` INT(11) NOT NULL DEFAULT '1'".
    ") ENGINE=InnoDB  DEFAULT CHARSET=utf8;".

    "CREATE TABLE IF NOT EXISTS `$this->dbname`.`$this->settings` (".
    "`DOMAIN` varchar(60) NOT NULL,".
    "`SLOGAN` varchar(255) NOT NULL,".
    "`EMAIL` varchar(100) NOT NULL".
    ") ENGINE=InnoDB  DEFAULT CHARSET=utf8;";

    if(!$result = $db->query($sql)){
        die('SQL['.$sql.']<br>There was an error running the query [' . $db->error . ']<br>');
    }

    echo "Installed!<br>";
}

Tried different quote marks, no luck. I also echoed the $sql, copied it and executed in phpmyadmin, it worked perfectly. But still it won't do anything inside of PHP. Thanks.

</div>
  • 写回答

1条回答 默认 最新

  • dpp89959 2017-04-18 19:43
    关注

    Use multi_query instead query, because you would like to multiple query not only one. As you can see the issue appeared right away after first query.

    http://php.net/manual/en/mysqli.multi-query.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?