我的实际csv文件中有时间戳,但在加载到mysql时,除了时间戳保持为空之外,每个东西都被加载。我在phpadmin(即xampp)中使用mysql,
使用的代码是:
<?php
// specify connection info
$connect = mysql_connect('localhost','root','');
if (!$connect)
{
die('Could not <span id="IL_AD1" class="IL_AD">
connect to</span> MySQL: ' . mysql_error());
}
$cid =mysql_select_db('test',$connect); //specify db name
define('CSV_PATH','C:\Users\vijay\Desktop');
$csv_file = CSV_PATH . "\probe.csv"; // Name of your CSV file
$csvfile = fopen($csv_file,"r");
$theData = fgets($csvfile);
$i = 0;
while (!feof($csvfile))
{
$csv_data[] = fgets($csvfile, 1024);
$csv_array = explode(",", $csv_data[$i]);
$insert_csv = array();
$insert_csv["app_name"] = $csv_array[0];
$insert_csv["user_id"] = $csv_array[1];
$insert_csv["timestamp"] = $csv_array[2];
$insert_csv["event"] = $csv_array[3];
$insert_csv["feature"] = $csv_array[4];
$insert_csv["action_name"] = $csv_array[5];
$insert_csv["qoe_rate"] = $csv_array[6];
$insert_csv["qoe_rate_description"] = $csv_array[7];
$query = "insert into csvdata2 (app_name,user_id,timestamp,event,feature,action_name,qoe_rate,qoe_rate_description) VALUES('$insert_csv[app_name]','$insert_csv[user_id]',$insert_csv[timestamp],'$insert_csv[event]','$insert_csv[feature]','$insert_csv[action_name]',$insert_csv[qoe_rate],'$insert_csv[qoe_rate_description]')";
$n=mysql_query($query);
echo $n;
$i++;
}
fclose($csvfile);
echo "file data successfully imported to database!!";
mysql_close($connect);
?>
如何包含时间戳代码,以便我可以获得加载到表中的确切时间戳数据而不会打扰其他条目...提前感谢
答案 0 :(得分:0)
你缺少引号
下面
$insert_csv[timestamp]
$insert_csv[qoe_rate]
立即尝试
$query = "insert into csvdata2 (app_name,user_id,timestamp,event,feature,action_name,qoe_rate,qoe_rate_description) VALUES('$insert_csv[app_name]','$insert_csv[user_id]','$insert_csv[timestamp]','$insert_csv[event]','$insert_csv[feature]','$insert_csv[action_name]','$insert_csv[qoe_rate]','$insert_csv[qoe_rate_description]')";
并按照@Plenka在评论中提到的内容