我在各处寻找可能的答案,但我不断得到同样的错误。
查询:
CREATE TABLE `example`.`profile` (
`type` VARCHAR(60) NOT NULL,
`optionalPassword` VARCHAR(30),
`dateFound` DATETIME NOT NULL DEFAULT NOW(),
`code` VARCHAR(15) NOT NULL,
`loggedOut` TINYINT(1) NOT NULL,
`banned` TINYINT(1) NOT NULL,
`GLOBALBan` TINYINT(1) NOT NULL,
`verifiedDate` DATETIME NOT NULL,
PRIMARY KEY (`code`)
);
CREATE TABLE `example`.`friends` (
`ID` VARCHAR(45) NOT NULL,
`nickName` VARCHAR(40),
`relation` VARCHAR(25),
`confirmed` TINYINT(1),
`FoundDate` DATETIME DEFAULT NOW(),
`anniversary` DATETIME,
`canWrite` TINYINT(1) default 1,
`canRead` TINYINT(1) default 1,
`collectedSubjects` VARCHAR(40),
`collectedInstitutions` VARCHAR(40),
`collectedWorkPlacement` VARCHAR(40),
PRIMARY KEY (`ID`)
);
我一直收到这个MySQL查询错误
1064:您的SQL语法有错误;检查手册 对应于您的MySQL服务器版本,以便使用正确的语法 'CREATE TABLE
example
附近。friends
(userID
VARCHAR(45)NOT 第13行为NULLnickName
V'
当我通过'php'运行它时,我只会遇到此错误如果我通过Oracle MySQL WorkBench运行完整查询它完美无缺,我无法理解它。
编辑:
$SpecificTableInstanceConn = Connect('localhost','example','user','') or die("cant connect");
$query = "
CREATE TABLE `$ID`.`profile` (
`type` VARCHAR(60) NOT NULL,
`optionalPassword` VARCHAR(30),
`dateFound` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`code` VARCHAR(15) NOT NULL,
`loggedOut` TINYINT(1) NOT NULL,
`banned` TINYINT(1) NOT NULL,
`GLOBALBan` TINYINT(1) NOT NULL,
`verifiedDate` TIMESTAMP,
PRIMARY KEY (`code`)
);
CREATE TABLE `$ID`.`friends` (
`ID` VARCHAR(45) NOT NULL,
`nickName` VARCHAR(40),
`relation` VARCHAR(25),
`confirmed` TINYINT(1),
`FoundDate` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`anniversary` TIMESTAMP,
`canWrite` TINYINT(1) default 1,
`canRead` TINYINT(1) default 1,
`collectedSubjects` VARCHAR(40),
`collectedInstitutions` VARCHAR(40),
`collectedWorkPlacement` VARCHAR(40),
PRIMARY KEY (`ID`)
);
";
echo $query;
$Results = Query($SpecificTableInstanceConn,$query) or die("cannot Create Databases");
查询和连接功能:
function Connect($db_host, $db_user, $db_pass, $db_name) {
$conn = mysqli_connect($db_host,$db_user,$db_pass) or die("Cannot connect :o");
if ($db_name != "") {
mysqli_select_db($conn, "$db_name") or die("Cannot connect :o");
}
return $conn;
}
function Query($conn, $Query){
$results = mysqli_query( $conn,$Query);
if (mysqli_errno($conn) != 0){
echo '<br />'.mysqli_errno($conn) . ": " . mysqli_error($conn) . "<br />";
}
mysqli_close($conn);
return $results;
}
答案 0 :(得分:1)
答案 1 :(得分:0)
mysql_*
不支持多个查询,您必须使用2个查询来创建表格或更改为MySQLi/PDO
库。
同样在您的牌桌上,如果您想要现在的默认值,则无法将DATETIME
与DEFAULT NOW()
一起使用TIMESTAMP
和CURRENT_TIMESTAMP
。
`dateFound` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
同样适用于
`FoundDate` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
在您的代码中,您需要更改:
$results = mysqli_query( $conn,$Query);
要:
$results = mysqli_multi_query( $conn,$Query);
或者你可以这样做:
$count = strlen($Query) - strlen(str_replace(';', '', $Query));
if ($count > 1)
$results = mysqli_multi_query( $conn,$Query);
else
$results = mysqli_query( $conn,$Query);
接受多个查询。