dtjpz48440 2015-11-14 12:45
浏览 26
已采纳

PHP代码中的SQL语法错误,即使命令在命令行中运行

I'm trying to make some SQL commands in XAMPP. My query returns following error:

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 kayttajat ( id INT(10) NOT NULL AUTO_INCREMENT, tunn' at line 2

I can't find the syntax error. And when I run the exact same command by copy-paste in command line, it works. So do I need some different syntax in PHP code? Also if I remove the first command, the error message moves to [...] right syntax to use near 'CREATE TABLE IF NOT EXISTS rivit [...]. If I remove second command the error comes from third command and so on. I really don't understand where the error is.

$query='
CREATE DATABASE IF NOT EXISTS asdgfhj;
CREATE TABLE IF NOT EXISTS kayttajat
(
    id INT(10) NOT NULL AUTO_INCREMENT,
    tunnus1 varchar(32) NOT NULL,
    tunnus2 varchar(32) NOT NULL,
    nimi varchar(32),
    nimi2 varchar(32),
    oikeus INT(10) NOT NULL DEFAULT 1,
    PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS rivit
(
    id INT(10) NOT NULL AUTO_INCREMENT,
    sivu INT(10) NOT NULL,
    kayttaja INT(10) NOT NULL,
    sana varchar(500),
    kommentti varchar(1000),
    aika TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    muutos TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY(id)
);
CREATE TABLE IF NOT EXISTS sivut
(
    id INT(10) NOT NULL AUTO_INCREMENT,
    nimi varchar(32) NOT NULL,
    ohje varchar(2000) NOT NULL,
    salaisuus INT(10) NOT NULL DEFAULT 1,
    PRIMARY KEY(id)
);
';

$mysqli->query($query) or die($mysqli->error);
  • 写回答

2条回答 默认 最新

  • douhao5280 2015-11-14 12:50
    关注

    The query() method is designed to execute a single query not multiple ones.

    What you are looking for is multi_query() to execute multiple queries separated by a semicolon.

    $query  = "SELECT CURRENT_USER();";
    $query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";
    
    /* execute multi query */
    if ($mysqli->multi_query($query)) {
        ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?