gpt4 book ai didi

php - SQL查询在php中不起作用,但在phpmyadmin中起作用

转载 作者:行者123 更新时间:2023-11-29 10:52:29 25 4
gpt4 key购买 nike

我正在摆弄 OOP mysqli。我偶然发现了这个问题:

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]

这是 PHP 代码:

    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>";
}

尝试了不同的引号,没有成功。我还回显了 $sql,复制它并在 phpmyadmin 中执行,它运行得很好。但它仍然不会在 PHP 内部执行任何操作。谢谢。

最佳答案

使用multi_query代替查询,因为你想要多个查询而不是只有一个。正如您所看到的,第一次查询后问题立即出现。

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

关于php - SQL查询在php中不起作用,但在phpmyadmin中起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43480769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com