在php生成的查询中获取mySQL错误

时间:2011-03-18 22:09:11

标签: 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 checklisttest; CREATE TABLE checklisttest ( Incident va' at line 2

以下是从php脚本中运行的一些查询:

    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());
}

2 个答案:

答案 0 :(得分:3)

一些错误:

  • 您有一个名为varchar的类型,但不允许这样做。您必须指定长度,例如varchar(128)
  • Actual不应该是AUTO_INCREMENT,因为它不是密钥的一部分,而且您已经有一个自动增量字段。
  • 而不是('20009',,'E02_04',...你可能意味着('20009',NULL,'E02_04',...(注意额外的NULL)。

答案 1 :(得分:2)

您必须为每个varchar字段指定长度。此外,您有两个auto_increment字段