sql->查询结果上的未知列

时间:2014-02-21 19:34:25

标签: php mysql mysqli

我的代码是:

if (($handle = fopen(self::UPLOAD_PATH . $this->_filename, "r")) !== false) {
$mysqlCols = array();
while (($data = fgetcsv($handle)) !== false) {
    $num = count($data);
    for ($c = 0; $c < $num; $c++) {

        if ($row == 0) {
            if (md5($data[$c]) == 'c7393c74ae43e690f8bbb78fcc4d9223') {
                $data[$c] = 'Unique ID'; // Fix due to an issue with wrong characters while reading...
            }
            $mysqlCols[$c] = $data[$c];
        } else {
            $mysqlValues[$c] = $mysqli->real_escape_string($data[$c]);
        }
    }

    if ($row > 0) {
        $query = "INSERT INTO `temporary_datas` (`" . implode('`, `', $mysqlCols) . "`) VALUES ('" . implode("', '", $mysqlValues) . "')";
        echo $query . ';<br>';
        $result = $mysqli->query($query) or die($mysqli->error);
        var_dump($result);
    }

    $row++;

    if ($count++ == 3) {
        break;
    }
}
fclose($handle);
} else {
    return false;
}

我的表:

    CREATE TABLE `temporary_datas` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `Unique ID` VARCHAR(50) NOT NULL DEFAULT '0',
    `Conversion Date` VARCHAR(50) NOT NULL DEFAULT '0',
    `Last Updated` VARCHAR(50) NOT NULL DEFAULT '0',
    `Click Date` VARCHAR(50) NOT NULL DEFAULT '0',
    `Affiliate ID` VARCHAR(50) NOT NULL DEFAULT '0',
    `Affiliate Name` VARCHAR(50) NOT NULL DEFAULT '0',
    `Advertiser ID` VARCHAR(50) NOT NULL DEFAULT '0',
    `Advertiser Name` VARCHAR(50) NOT NULL DEFAULT '0',
    `Offer ID` VARCHAR(50) NOT NULL DEFAULT '0',
    `Offer Name` VARCHAR(50) NOT NULL DEFAULT '0',
    `Creative` VARCHAR(50) NOT NULL DEFAULT '0',
    `Sub ID` VARCHAR(50) NOT NULL DEFAULT '0',
    `Sub ID 2` VARCHAR(50) NOT NULL DEFAULT '0',
    `Sub ID 3` VARCHAR(50) NOT NULL DEFAULT '0',
    `Sub ID 4` VARCHAR(50) NOT NULL DEFAULT '0',
    `Sub ID 5` VARCHAR(50) NOT NULL DEFAULT '0',
    `Type` VARCHAR(50) NOT NULL DEFAULT '0',
    `Paid` VARCHAR(50) NOT NULL DEFAULT '0',
    `Price Paid Currency` VARCHAR(50) NOT NULL DEFAULT '0',
    `Received` VARCHAR(50) NOT NULL DEFAULT '0',
    `Price Received Currency` VARCHAR(50) NOT NULL DEFAULT '0',
    `Reached Step` VARCHAR(50) NOT NULL DEFAULT '0',
    `Pixel` VARCHAR(50) NOT NULL DEFAULT '0',
    `Throttled` VARCHAR(50) NOT NULL DEFAULT '0',
    `Returned` VARCHAR(50) NOT NULL DEFAULT '0',
    `Test` VARCHAR(50) NOT NULL DEFAULT '0',
    `Transaction ID` VARCHAR(50) NOT NULL DEFAULT '0',
    `IP Address` VARCHAR(50) NOT NULL DEFAULT '0',
    `Click IP Address` VARCHAR(50) NOT NULL DEFAULT '0',
    `Country` VARCHAR(50) NOT NULL DEFAULT '0',
    `Conversion Referrer` VARCHAR(50) NOT NULL DEFAULT '0',
    `Referrer` VARCHAR(50) NOT NULL DEFAULT '0',
    `Conversion User Agent` VARCHAR(50) NOT NULL DEFAULT '0',
    `Click User Agent` VARCHAR(50) NOT NULL DEFAULT '0',
    `Note` VARCHAR(50) NOT NULL DEFAULT '0',
    `Approved` VARCHAR(50) NOT NULL DEFAULT '0',
    `Disposition` VARCHAR(50) NOT NULL DEFAULT '0',
    `Region` VARCHAR(50) NOT NULL DEFAULT '0',
    `Language` VARCHAR(50) NOT NULL DEFAULT '0',
    `Provider Name` VARCHAR(50) NOT NULL DEFAULT '0',
    `Device` VARCHAR(50) NOT NULL DEFAULT '0',
    `Operating System` VARCHAR(50) NOT NULL DEFAULT '0',
    `OS (Major Version)` VARCHAR(50) NOT NULL DEFAULT '0',
    `OS (Minor Version)` VARCHAR(50) NOT NULL DEFAULT '0',
    `Browser` VARCHAR(50) NOT NULL DEFAULT '0',
    `Browser (Major Version)` VARCHAR(50) NOT NULL DEFAULT '0',
    `Browser (Minor Version)` VARCHAR(50) NOT NULL DEFAULT '0',
    `Conversion Score` VARCHAR(50) NOT NULL DEFAULT '0',
    `Paid Unbilled` VARCHAR(50) NOT NULL DEFAULT '0',
    `Received Unbilled` VARCHAR(50) NOT NULL DEFAULT '0',
    `Click Request Session ID` VARCHAR(50) NOT NULL DEFAULT '0',
    `Event Name` VARCHAR(50) NOT NULL DEFAULT '0',
    `Price Format` VARCHAR(50) NOT NULL DEFAULT '0',
    PRIMARY KEY (`id`)
) COLLATE='utf8_general_ci'
    ENGINE=InnoDB
    AUTO_INCREMENT=10;

我的结果是(var_dump one):

INSERT INTO `temporary_datas` (`Unique ID`, `Conversion Date`, `Last Updated`, `Click Date`, `Affiliate ID`, `Affiliate Name`, `Advertiser ID`, `Advertiser Name`, `Offer ID`, `Offer Name`, `Creative`, `Sub ID`, `Sub ID 2`, `Sub ID 3`, `Sub ID 4`, `Sub ID 5`, `Type`, `Paid`, `Price Paid Currency`, `Received`, `Price Received Currency`, `Reached Step`, `Pixel`, `Throttled`, `Returned`, `Test`, `Transaction ID`, `IP Address`, `Click IP Address`, `Country`, `Conversion Referrer`, `Referrer`, `Conversion User Agent`, `Click User Agent`, `Note`, `Approved`, `Disposition`, `Region`, `Language`, `Provider Name`, `Device`, `Operating System`, `OS (Major Version)`, `OS (Minor Version)`, `Browser`, `Browser (Major Version)`, `Browser (Minor Version)`, `Conversion Score`, `Paid Unbilled`, `Received Unbilled`, `Click Request Session ID`, `Event Name`, `Price Format`) VALUES ('1258806', '02/01/2014 2:40:05 PM', '02/01/2014 2:46:22 PM', '02/01/2014 2:28:44 PM', '120958', 'iTap Ads', '163', 'Zinq Media', '3002', '[WAP] Android Clean-up - ES', '37670', '', 'cU2U3A68JPK94J990MAK5QFRKD', '{gclid}', '849d37fa-1c14-466e-87c1-2325c23d073c', '', 'Click', '10.7000', 'USD', '13.3800', 'USD', '1', 'True', 'False', 'False', 'False', '', '95.138.134.110', '46.222.188.248', 'ES', '', 'http://droid-safety.com/sp/scan1/?voluumdata=vid..00000305-cc9e-4715-8000-000000000000__vpid..0c977800-8b73-11e3-8759-c26a9a686c33__caid..c1c24ba3-cbe3-470d-9e8f-192a66242968__lid..849d37fa-1c14-466e-87c1-2325c23d073c__oid1..1ac7589d-b050-4ec2-8e03-3cf386', 'CAKE: (getcake.com): Tracking Service', 'Mozilla/5.0 (Linux; Android 4.3; ka-ge; SAMSUNG GT-I9505 Build/JSS15J) AppleWebKit/537.36 (KHTML, like Gecko) Version/1.5 Chrome/28.0.1500.94 Mobile Safari/537.36', '', 'False', '', 'Madrid', 'GEORGIAN', 'xfera moviles s.a.', '', 'Android', '4', '4.3', 'Chrome', 'Other', '', '0.00', '0.0000', '13.3800', '29699629', '', 'CPA\r');
  

“字段列表”中的未知列“价格格式”

如果我接受这个查询并直接通过mysql运行它,它可以正常工作。我不明白什么是错的。有什么想法吗?


int 53
int 53
array (size=53)  0 => string 'Unique ID' (length=9)  1 => string 'Conversion Date' (length=15)  2 => string 'Last Updated' (length=12)  3 => string 'Click Date' (length=10)  4 => string 'Affiliate ID' (length=12)  5 => string 'Affiliate Name' (length=14)  6 => string 'Advertiser ID' (length=13)  7 => string 'Advertiser Name' (length=15)  8 => string 'Offer ID' (length=8)  9 => string 'Offer Name' (length=10)  10 => string 'Creative' (length=8)  11 => string 'Sub ID' (length=6)  12 => string 'Sub ID 2' (length=8)  13 => string 'Sub ID 3' (length=8)  14 => string 'Sub ID 4' (length=8)  15 => string 'Sub ID 5' (length=8)  16 => string 'Type' (length=4)  17 => string 'Paid' (length=4)  18 => string 'Price Paid Currency' (length=19)  19 => string 'Received' (length=8)  20 => string 'Price Received Currency' (length=23)  21 => string 'Reached Step' (length=12)  22 => string 'Pixel' (length=5)  23 => string 'Throttled' (length=9)  24 => string 'Returned' (length=8)  25 => string 'Test' (length=4)  26 => string 'Transaction ID' (length=14)  27 => string 'IP Address' (length=10)  28 => string 'Click IP Address' (length=16)  29 => string 'Country' (length=7)  30 => string 'Conversion Referrer' (length=19)  31 => string 'Referrer' (length=8)  32 => string 'Conversion User Agent' (length=21)  33 => string 'Click User Agent' (length=16)  34 => string 'Note' (length=4)  35 => string 'Approved' (length=8)  36 => string 'Disposition' (length=11)  37 => string 'Region' (length=6)  38 => string 'Language' (length=8)  39 => string 'Provider Name' (length=13)  40 => string 'Device' (length=6)  41 => string 'Operating System' (length=16)  42 => string 'OS (Major Version)' (length=18)  43 => string 'OS (Minor Version)' (length=18)  44 => string 'Browser' (length=7)  45 => string 'Browser (Major Version)' (length=23)  46 => string 'Browser (Minor Version)' (length=23)  47 => string 'Conversion Score' (length=16)  48 => string 'Paid Unbilled' (length=13)  49 => string 'Received Unbilled' (length=17)  50 => string 'Click Request Session ID' (length=24)  51 => string 'Event Name' (length=10)  52 => string 'Price Format
' (length=13)
array (size=53)  0 => string '1258806' (length=7)  1 => string '02/01/2014 2:40:05 PM' (length=21)  2 => string '02/01/2014 2:46:22 PM' (length=21)  3 => string '02/01/2014 2:28:44 PM' (length=21)  4 => string '120958' (length=6)  5 => string 'iTap Ads' (length=8)  6 => string '163' (length=3)  7 => string 'Zinq Media' (length=10)  8 => string '3002' (length=4)  9 => string '[WAP] Android Clean-up - ES' (length=27)  10 => string '37670' (length=5)  11 => string '' (length=0)  12 => string 'cU2U3A68JPK94J990MAK5QFRKD' (length=26)  13 => string '{gclid}' (length=7)  14 => string '849d37fa-1c14-466e-87c1-2325c23d073c' (length=36)  15 => string '' (length=0)  16 => string 'Click' (length=5)  17 => string '10.7000' (length=7)  18 => string 'USD' (length=3)  19 => string '13.3800' (length=7)  20 => string 'USD' (length=3)  21 => string '1' (length=1)  22 => string 'True' (length=4)  23 => string 'False' (length=5)  24 => string 'False' (length=5)  25 => string 'False' (length=5)  26 => string '' (length=0)  27 => string '95.138.134.110' (length=14)  28 => string '46.222.188.248' (length=14)  29 => string 'ES' (length=2)  30 => string '' (length=0)  31 => string 'http://droid-safety.com/sp/scan1/?voluumdata=vid..00000305-cc9e-4715-8000-000000000000__vpid..0c977800-8b73-11e3-8759-c26a9a686c33__caid..c1c24ba3-cbe3-470d-9e8f-192a66242968__lid..849d37fa-1c14-466e-87c1-2325c23d073c__oid1..1ac7589d-b050-4ec2-8e03-3cf386' (length=255)  32 => string 'CAKE: (getcake.com): Tracking Service' (length=37)  33 => string 'Mozilla/5.0 (Linux; Android 4.3; ka-ge; SAMSUNG GT-I9505 Build/JSS15J) AppleWebKit/537.36 (KHTML, like Gecko) Version/1.5 Chrome/28.0.1500.94 Mobile Safari/537.36' (length=162)  34 => string '' (length=0)  35 => string 'False' (length=5)  36 => string '' (length=0)  37 => string 'Madrid' (length=6)  38 => string 'GEORGIAN' (length=8)  39 => string 'xfera moviles s.a.' (length=18)  40 => string '' (length=0)  41 => string 'Android' (length=7)  42 => string '4' (length=1)  43 => string '4.3' (length=3)  44 => string 'Chrome' (length=6)  45 => string 'Other' (length=5)  46 => string '' (length=0)  47 => string '0.00' (length=4)  48 => string '0.0000' (length=6)  49 => string '13.3800' (length=7)  50 => string '29699629' (length=8)  51 => string '' (length=0)  52 => string 'CPA\r' (length=5)
INSERT INTO `temporary_datas` (`Unique ID`, `Conversion Date`, `Last Updated`, `Click Date`, `Affiliate ID`, `Affiliate Name`, `Advertiser ID`, `Advertiser Name`, `Offer ID`, `Offer Name`, `Creative`, `Sub ID`, `Sub ID 2`, `Sub ID 3`, `Sub ID 4`, `Sub ID 5`, `Type`, `Paid`, `Price Paid Currency`, `Received`, `Price Received Currency`, `Reached Step`, `Pixel`, `Throttled`, `Returned`, `Test`, `Transaction ID`, `IP Address`, `Click IP Address`, `Country`, `Conversion Referrer`, `Referrer`, `Conversion User Agent`, `Click User Agent`, `Note`, `Approved`, `Disposition`, `Region`, `Language`, `Provider Name`, `Device`, `Operating System`, `OS (Major Version)`, `OS (Minor Version)`, `Browser`, `Browser (Major Version)`, `Browser (Minor Version)`, `Conversion Score`, `Paid Unbilled`, `Received Unbilled`, `Click Request Session ID`, `Event Name`, `Price Format`) VALUES ('1258806', '02/01/2014 2:40:05 PM', '02/01/2014 2:46:22 PM', '02/01/2014 2:28:44 PM', '120958', 'iTap Ads', '163', 'Zinq Media', '3002', '[WAP] Android Clean-up - ES', '37670', '', 'cU2U3A68JPK94J990MAK5QFRKD', '{gclid}', '849d37fa-1c14-466e-87c1-2325c23d073c', '', 'Click', '10.7000', 'USD', '13.3800', 'USD', '1', 'True', 'False', 'False', 'False', '', '95.138.134.110', '46.222.188.248', 'ES', '', 'http://droid-safety.com/sp/scan1/?voluumdata=vid..00000305-cc9e-4715-8000-000000000000__vpid..0c977800-8b73-11e3-8759-c26a9a686c33__caid..c1c24ba3-cbe3-470d-9e8f-192a66242968__lid..849d37fa-1c14-466e-87c1-2325c23d073c__oid1..1ac7589d-b050-4ec2-8e03-3cf386', 'CAKE: (getcake.com): Tracking Service', 'Mozilla/5.0 (Linux; Android 4.3; ka-ge; SAMSUNG GT-I9505 Build/JSS15J) AppleWebKit/537.36 (KHTML, like Gecko) Version/1.5 Chrome/28.0.1500.94 Mobile Safari/537.36', '', 'False', '', 'Madrid', 'GEORGIAN', 'xfera moviles s.a.', '', 'Android', '4', '4.3', 'Chrome', 'Other', '', '0.00', '0.0000', '13.3800', '29699629', '', 'CPA\r');
Unknown column 'Price Format' in 'field list'

2 个答案:

答案 0 :(得分:1)

它可能是空格问题。

  • 选择列

    时检查是否没有空格
     `Price Format `
                  ^----//space here
    

检查this demo

  • 在表格中检查列在列之前或之后没有空格。

    $query = "INSERT INTO `temporary_datas` (`" . implode('`, `', $mysqlCols) . "`)
            VALUES ('" . implode("', '", $mysqlValues) . "') ";
    

添加此

  $data[$c] = trim($data[$c], " \r");

答案 1 :(得分:0)

没有完成整个过程,快速建议是在“CPA \ r”的最终值中尝试没有“\ r”,因为在PHP中它是回车的转义序列并且可能导致问题

编辑:我认为内容包括:

'Mozilla/5.0 (Linux; Android 4.3; ka-ge; SAMSUNG GT-I9505 Build/JSS15J) AppleWebKit/537.36 (KHTML, like Gecko) Version/1.5 Chrome/28.0.1500.94 Mobile Safari/537.36'

- 所以它以逗号分隔:

" 'Mozilla/5.0 (Linux; Android 4.3; ka-ge; SAMSUNG GT-I9505 Build/JSS15J) AppleWebKit/537.36 (KHTML"
" like Gecko) Version/1.5 Chrome/28.0.1500.94 Mobile Safari/537.36'"

因此,您需要为fgetcsv()指定字段外壳字符。 E.g。

$data = fgetcsv($handle, 1000, ",", "'")

在插入SQL $查询时,您可能需要添加单引号。

编辑2: 尝试使用它进行调试:

/*
if (($handle = fopen(self::UPLOAD_PATH . $this->_filename, "r")) !== false) {
$mysqlCols = array();
while (($data = fgetcsv($handle)) !== false) {
    $num = count($data);
    for ($c = 0; $c < $num; $c++) {

        if ($row == 0) {
            if (md5($data[$c]) == 'c7393c74ae43e690f8bbb78fcc4d9223') {
                $data[$c] = 'Unique ID'; // Fix due to an issue with wrong characters while reading...
            }
            $mysqlCols[$c] = $data[$c];
        } else {
            $mysqlValues[$c] = $mysqli->real_escape_string($data[$c]);
        }
    }

    if ($row > 0) {*/
        #$query = "INSERT INTO `temporary_datas` (`" . implode('`, `', $mysqlCols) . "`) VALUES ('" . implode("', '", $mysqlValues) . "')";
        $query = "INSERT INTO `temporary_datas` (`Unique ID`, `Conversion Date`, `Last Updated`, `Click Date`, `Affiliate ID`, `Affiliate Name`, `Advertiser ID`, `Advertiser Name`, `Offer ID`, `Offer Name`, `Creative`, `Sub ID`, `Sub ID 2`, `Sub ID 3`, `Sub ID 4`, `Sub ID 5`, `Type`, `Paid`, `Price Paid Currency`, `Received`, `Price Received Currency`, `Reached Step`, `Pixel`, `Throttled`, `Returned`, `Test`, `Transaction ID`, `IP Address`, `Click IP Address`, `Country`, `Conversion Referrer`, `Referrer`, `Conversion User Agent`, `Click User Agent`, `Note`, `Approved`, `Disposition`, `Region`, `Language`, `Provider Name`, `Device`, `Operating System`, `OS (Major Version)`, `OS (Minor Version)`, `Browser`, `Browser (Major Version)`, `Browser (Minor Version)`, `Conversion Score`, `Paid Unbilled`, `Received Unbilled`, `Click Request Session ID`, `Event Name`, `Price Format`) VALUES ('1258806', '02/01/2014 2:40:05 PM', '02/01/2014 2:46:22 PM', '02/01/2014 2:28:44 PM', '120958', 'iTap Ads', '163', 'Zinq Media', '3002', '[WAP] Android Clean-up - ES', '37670', '', 'cU2U3A68JPK94J990MAK5QFRKD', '{gclid}', '849d37fa-1c14-466e-87c1-2325c23d073c', '', 'Click', '10.7000', 'USD', '13.3800', 'USD', '1', 'True', 'False', 'False', 'False', '', '95.138.134.110', '46.222.188.248', 'ES', '', 'http://droid-safety.com/sp/scan1/?voluumdata=vid..00000305-cc9e-4715-8000-000000000000__vpid..0c977800-8b73-11e3-8759-c26a9a686c33__caid..c1c24ba3-cbe3-470d-9e8f-192a66242968__lid..849d37fa-1c14-466e-87c1-2325c23d073c__oid1..1ac7589d-b050-4ec2-8e03-3cf386', 'CAKE: (getcake.com): Tracking Service', 'Mozilla/5.0 (Linux; Android 4.3; ka-ge; SAMSUNG GT-I9505 Build/JSS15J) AppleWebKit/537.36 (KHTML, like Gecko) Version/1.5 Chrome/28.0.1500.94 Mobile Safari/537.36', '', 'False', '', 'Madrid', 'GEORGIAN', 'xfera moviles s.a.', '', 'Android', '4', '4.3', 'Chrome', 'Other', '', '0.00', '0.0000', '13.3800', '29699629', '', 'CPA\r');";
        echo $query . ';<br>';
        $result = $mysqli->query($query) or die($mysqli->error);
        var_dump($result);
    /*}

    $row++;

    if ($count++ == 3) {
        break;
    }
}
fclose($handle);
} else {
    return false;
}
*/