我一直在從cdr中檢索通話記錄並將它們轉儲到MySQL中的數據庫中。最近數據庫崩潰,並給我重複和垃圾字符,所以我修改爲下面的代碼。 修改後的代碼獲取錯誤'錯誤:INSERT INTO`call`.`logs`(id,hashkey)VALUES(,XXX,)'
$file1 = file_get_contents('file:///C:/Users/thy/Desktop/2011_0419_1531_v3.12R/cdr/'.$newname, FILE_USE_INCLUDE_PATH);
$arr1 = explode("\n", $file1);
foreach ($arr1 as $key => $value) {
$colArray = [];
$colArray['id'] = null;
$colArray['hashkey'] = md5(uniqid(rand(), true));
$split = explode(";", $value);
foreach ($split as $key => $val) {
# code...
$arr = (explode('=', $val));
$field = 'ch';
$item = '0';
$field = $arr[0];
$item = $arr[1];
$item = str_replace(str_split(')(\/'), '', $item);
$colArray[$field] = $item;
}
$columns = implode(', ', array_keys($colArray));
$values = implode(', ', $colArray);
$sql = "INSERT INTO `call`.`logs` (" . $columns . ") VALUES (" . $values . ")";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
上述代碼不斷給我錯誤
Error: INSERT INTO
call
.logs
(id, hashkey,) VALUES (, 797d8782a433b30e196fafc0ce01d09b,)
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 ') VALUES (, 797d8782a433b30e196fafc0ce01d09b,)' at line 1
原始代碼是下面(一個我改性它從)
$文件1 =的file_get_contents(」 file:/// C:/Users/thy/Desktop/2011_0419_1531_v3.12R/cdr/'.$newname,FILE_USE_INCLUDE_PATH); $ arr1 =爆炸(「\ n」,$ file1);
$data1 = array();
foreach ($arr1 as $key => $value) {
$split = explode(";", $value);
$keys = md5(uniqid(rand(), true));
//insert key to identify call.
$sql = "INSERT INTO `call`.`logs` (`id`, `hashkey`) VALUES (NULL, '{$keys}')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
foreach ($split as $key => $val) {
# code...
$arr=(explode('=', $val));
$field='ch';
$item='0';
$field=$arr[0];
$item=$arr[1];
echo $field . " --";
echo "<br/>";
echo $item;
//sql
$sql = "UPDATE logs SET {$field}='{$item}' WHERE hashkey='{$keys}'";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
echo "done";
//$conn->close();
} echo "<br/>";
}
?>
<?php
什麼是DB表日誌的結構定義? – Rizwan
你是什麼意思? – xbile
@xbile。我的答案解決了你的問題?請讓我知道試圖以其他方式幫助你,如果沒有... –