出于某种原因,在PHP中,implode
函数在返回字符串的末尾从$tmpTblRow
返回一个额外的字段,并导致MySQL错误。
MySQL语句来自:
$sqll = sprintf(
"INSERT INTO $sqlToTbl (%s) VALUES ('%s')",
$sqlToCols,
implode("','", array_values($tmpTblRow))
);
$ tmpTblRow是关联数组:
[lineItem] => null
[partID] => 1
[partNumber] => tr2-mod2-0001
[serialNumber] =>
[partDescription] => mob176wertyu
[quantity] => 10
[price] => 500
[warranty] =>
[dateRequired] => 2055-11-11
[note] =>
[discount] =>
[isProcessed] => 1
[parameter] =>
[isPrivate] =>
[processedByUserID] => 1
[processedDate] => 2013-02-04
[extPrice] => 0
[parentID] => 36
[isToAct] => 1
[userID] => 0
[type] =>
[dateGenerated] => 2013-02-04 10:53:12
[unitType] =>
$ sqll VALUE RETURNS:
INSERT INTO tblOrdersItems
( lineItem, partID , partNumber , serialNumber, partDescription , quantity,
price , warranty , dateRequired , note , discount, isProcessed, parameter,
isPrivate, processedByUserID, processedDate, extPrice, parentID, isToAct,
userID, type, dateGenerated, unitType)
VALUES
( 'null', '1', 'tr2-mod2-0001', '', 'mob176wertyu', '10', '500', '',
'2055-11-11', '', '', '1', '', '', '1', '2013-02-04', '0', '36', '1', '0',
'', '2013-02-04 10:53:12', '', '1' )
返回错误是:“列数与第1行的值计数不匹配”
注意字符串1
末尾的额外$sqll
。任何人都可以解释为什么以及如何解决这个问题?
编辑... $ tmpTblRow的var_dump返回:
array(23) {
["lineItem"]=>
string(4) "null"
["partID"]=>
string(1) "1"
["partNumber"]=>
string(13) "tr2-mod2-0001"
["serialNumber"]=>
string(0) ""
["partDescription"]=>
string(12) "mob176wertyu"
["quantity"]=>
string(2) "10"
["price"]=>
string(3) "500"
["warranty"]=>
NULL
["dateRequired"]=>
string(10) "2055-11-11"
["note"]=>
NULL
["discount"]=>
NULL
["isProcessed"]=>
string(1) "1"
["parameter"]=>
NULL
["isPrivate"]=>
NULL
["processedByUserID"]=>
string(1) "1"
["processedDate"]=>
string(10) "2013-02-04"
["extPrice"]=>
string(1) "0"
["parentID"]=>
string(2) "36"
["isToAct"]=>
string(1) "1"
["userID"]=>
string(1) "0"
["type"]=>
string(0) ""
["dateGenerated"]=>
string(19) "2013-02-04 10:53:12"
["unitType"]=>
string(0) ""
}
......完整的代码就是这样:
$q = "SELECT $sqlFromCols FROM $sqlFromTbl $sqlFromWhere";
$result1 = $conn->query(stripslashes($q)) ;
if (!$result1) die($conn->error.">>none<< 1");
echo $q."||";
echo "<br/>start>";
while($tmpTblRow = $result1->fetch_array(MYSQL_ASSOC) )
{
var_dump ($tmpTblRow);
echo "<br/>end|";
echo "<br/>";
$tmpTblRow[$sqlFromIDcol] = $newID;
$sqll = sprintf("INSERT INTO $sqlToTbl (%s) VALUES ('%s')", $sqlFromCols, implode("','",array_values($tmpTblRow)));
echo $sqll."||";
$result = $conn->query(stripslashes($sqll)) ;
if (!$result) die($conn->error.">>none<< while");
$q = "UPDATE $sqlToTbl SET $change WHERE $sqlToIDcol = $newID";
$q = stripslashes($q);
$result = $conn->query(stripslashes($q)) ;
if (!$result) die($conn->error.">>none<< update");
//mysql_query($q);
//echo $q."||<br>";
$newID = $newID+1;
}
答案 0 :(得分:1)
如果我让这段代码运行:
$tmpTblRow = array(
'lineItem' => null,
'partID' => 1,
'partNumber' => 'tr2-mod2-0001',
'serialNumber' => '',
'partDescription' => 'mob176wertyu',
'quantity' => 10,
'price' => 500,
'warranty' => '',
'dateRequired' => '2055-11-11',
'note' => '',
'discount' => '',
'isProcessed' => 1,
'parameter' => '',
'isPrivate' => '',
'processedByUserID' => 1,
'processedDate' => '2013-02-04',
'extPrice' => 0,
'parentID' => 36,
'isToAct' => 1,
'userID' => 0,
'type' => '',
'dateGenerated' => '2013-02-04 10:53:12',
'unitType' => '',
);
$sqlToCols = "col, col, col";
$sqll = sprintf(
"INSERT INTO $sqlToTbl (%s) VALUES ('%s')",
$sqlToCols,
implode("','", array_values($tmpTblRow))
);
var_dump($sqll);
我得到了结果:
string(183) "INSERT INTO (col, col, col) VALUES ('','1','tr2-mod2-0001','','mob176wertyu','10','500','','2055-11-11','','','1','','','1','2013-02-04','0','36','1','0','','2013-02-04 10:53:12','')"
正如你所看到的,最后没有1。这只能意味着一件事:调试时没有转储正确的数组。