在php生成的查询中获取mySQL错误
让我再试一次,因为我不确定人们是否知道我在用php运行这个查询。php函数输出的查询在常规mySQL程序(如Navicat)中运行良好,但从php脚本运行时,在我的浏览器中抛出此错误:在php生成的查询中获取mySQL错误,php,mysql,Php,Mysql,让我再试一次,因为我不确定人们是否知道我在用php运行这个查询。php函数输出的查询在常规mySQL程序(如Navicat)中运行良好,但从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 'DROP TABLE IF EXISTS c
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 'DROP TABLE IF EXISTS checklisttest; CREATE TABLE checklisttest ( Incident va' at line 2
SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS checklisttest;
CREATE TABLE checklisttest (
Incident varchar(12) NOT NULL,
TestID mediumint(9) NOT NULL AUTO_INCREMENT,
Element varchar(12) NOT NULL,
Name varchar(128) NOT NULL,
Code varchar(512) NOT NULL,
Expected varchar(512) NOT NULL,
Actual varchar(512) NOT NULL,
AutoVerifyResult varchar(32) NOT NULL,
QAResult varchar(32) DEFAULT NULL,
Comments text,
PRIMARY KEY (TestID)
);
INSERT INTO `checklistTest` VALUES ('20009',NULL,'E02_04','Type of Service Requested','30','911 Response (Scene)','911 RESPONSE (SCENE)','100','Pass',NULL);
INSERT INTO `checklistTest` VALUES ('20009',NULL,'E02_11','EMS Unit/Vehicle Number','Medic 81','Medic 81','Medic 81','100','Pass',NULL);
INSERT INTO `checklistTest` VALUES ('20009',NULL,'E02_18','Patient Destination Odometer Reading of Responding Vehicle','11','11','Not set!','0','Fail',NULL);
INSERT INTO `checklistTest` VALUES ('20009',NULL,'E03_01','Complaint Reported by Dispatch','410','Animal Bite','ANIMAL BITE','100','Pass',NULL);
INSERT INTO `checklistTest` VALUES ('20009',NULL,'E04_01','Crew Member ID','EMT302875','EMT302875','EMT302875','100','Pass',NULL);
INSERT INTO `checklistTest` VALUES ('20009',NULL,'E04_01','Crew Member ID','PMD509465','PMD509465','PMD509465','100','Pass',NULL);
以下是从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 'DROP TABLE IF EXISTS checklisttest; CREATE TABLE checklisttest ( Incident va' at line 2
SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS checklisttest;
CREATE TABLE checklisttest (
Incident varchar(12) NOT NULL,
TestID mediumint(9) NOT NULL AUTO_INCREMENT,
Element varchar(12) NOT NULL,
Name varchar(128) NOT NULL,
Code varchar(512) NOT NULL,
Expected varchar(512) NOT NULL,
Actual varchar(512) NOT NULL,
AutoVerifyResult varchar(32) NOT NULL,
QAResult varchar(32) DEFAULT NULL,
Comments text,
PRIMARY KEY (TestID)
);
INSERT INTO `checklistTest` VALUES ('20009',NULL,'E02_04','Type of Service Requested','30','911 Response (Scene)','911 RESPONSE (SCENE)','100','Pass',NULL);
INSERT INTO `checklistTest` VALUES ('20009',NULL,'E02_11','EMS Unit/Vehicle Number','Medic 81','Medic 81','Medic 81','100','Pass',NULL);
INSERT INTO `checklistTest` VALUES ('20009',NULL,'E02_18','Patient Destination Odometer Reading of Responding Vehicle','11','11','Not set!','0','Fail',NULL);
INSERT INTO `checklistTest` VALUES ('20009',NULL,'E03_01','Complaint Reported by Dispatch','410','Animal Bite','ANIMAL BITE','100','Pass',NULL);
INSERT INTO `checklistTest` VALUES ('20009',NULL,'E04_01','Crew Member ID','EMT302875','EMT302875','EMT302875','100','Pass',NULL);
INSERT INTO `checklistTest` VALUES ('20009',NULL,'E04_01','Crew Member ID','PMD509465','PMD509465','PMD509465','100','Pass',NULL);
以下是生成该查询的php:
private function toDB(){
$sql = "
SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS checklisttest;
CREATE TABLE checklisttest (
Incident varchar(12) NOT NULL,
TestID mediumint(9) NOT NULL AUTO_INCREMENT,
Element varchar(12) NOT NULL,
Name varchar(128) NOT NULL,
Code varchar(512) NOT NULL,
Expected varchar(512) NOT NULL,
Actual varchar(512) NOT NULL,
AutoVerifyResult varchar(32) NOT NULL,
QAResult varchar(32) DEFAULT NULL,
Comments text,
PRIMARY KEY (TestID)
);
";
//iterate through the records $this->records[10001]
foreach($this->records as $inc => $record){
//iterate through the element ids $this->records[10001][E02_04]
foreach($this->records[$inc]["Elements"] as $elementID => $element){
//iterate through the element ids $this->records[10001][E02_04][1]
foreach($element as $key => $val){
$sql .= "
INSERT INTO `checklistTest` VALUES (\"$inc\",NULL,\"$elementID\",\"$val[name]\",\"$val[code]\",\"$val[expected]\",\"$val[actual]\",\"$val[match]\",\"$val[QAResult]\",NULL);";
}
}
}
echo "<textarea style='width:100%;height:400px'>$sql</textarea>";
mysql_select_db("new",$GLOBALS['local']);
mysql_query($sql,$GLOBALS['local']) or die(mysql_error());
}
private函数toDB(){
$sql=”
设置外键检查=0;
如果存在检查列表测试,则删除表;
创建表checklisttest(
事件varchar(12)不为空,
TestID mediumint(9)非空自动增量,
元素varchar(12)不为空,
名称varchar(128)不为空,
代码varchar(512)不为空,
应为varchar(512)不为空,
实际varchar(512)不为空,
自动验证结果varchar(32)不为空,
QASULT varchar(32)默认为空,
评论案文,
主键(TestID)
);
";
//遍历记录$this->records[10001]
foreach($this->记录为$inc=>$record){
//迭代元素ID$this->记录[10001][E02\u 04]
foreach($this->将[$inc][“元素”]记录为$elementID=>$element){
//迭代元素id$this->records[10001][E02_04][1]
foreach($key=>$val的元素){
$sql.=”
在“checklistTest”值中插入(\“$inc\”,NULL,\“$elementID\”,\“$val[name]\”,\“$val[code]\”,\“$val[expected]\”,\“$val[actual]\”,\“$val[match]\”,\“$val[QAResult]\”,NULL);”;
}
}
}
回显“$sql”;
mysql_select_db(“新的,$GLOBALS['local']);
mysql_query($sql,$GLOBALS['local'])或die(mysql_error());
}
一些错误:
- 您有一个名为
的类型,但这是不允许的。必须指定长度,例如varchar
varchar(128)
- 列
不应该是Actual
,因为它不是键的一部分,并且您已经有了一个AUTO INCREMENT字段AUTO_INCREMENT
- 与
不同,您的意思可能是('20009','E02_04',…
(请注意额外的('20009',NULL,'E02_04',…
)NULL