今天是个好日子,
老实说,我被困住了...我想寻求您的帮助。我面临的挑战是这样的: 1. 将 CSV 文件上传到服务器。2. 打开 CSV 文件。3. 解析它,显示数据并为 csv 文件的每条记录构建一个 sql 查询。
现在,我的问题是第 3 步。我已经在表格中解析和显示数据,但构建查询失败。任何帮助在这里表示赞赏!
这是我用来解析 csv 文件并显示数据的代码。我想寻求您的帮助,我应该如何更改代码以为文件的每条记录创建和执行 sql 查询。
if ( $file = fopen( "upload/" . $storagename , r ) ) {
echo "File opened.<br />";
$firstline = fgets ($file, 4096 );
//Gets the number of fields, in CSV-files the names of the fields are mostly given in the first line
$num = strlen($firstline) - strlen(str_replace(";", "", $firstline));
//save the different fields of the firstline in an array called fields
$fields = array();
$fields = explode( ";", $firstline, ($num+1) );
$line = array();
$i = 0;
//CSV: one line is one record and the cells/fields are seperated by ";"
//so $dsatz is an two dimensional array saving the records like this: $dsatz[number of record][number of cell]
while ( $line[$i] = fgets ($file, 4096) ) {
$dsatz[$i] = array();
$dsatz[$i] = explode( ";", $line[$i], ($num+1) );
$i++;
}
echo "<table>";
echo "<tr>";
for ( $k = 0; $k != ($num+1); $k++ ) {
echo "<td>" . $fields[$k] . "</td>";
}
echo "</tr>";
foreach ($dsatz as $key => $number) {
//new table row for every record
echo "Record <tr>";
foreach ($number as $k => $content) {
//new table cell for every field of the record
echo "<td>" . $content . " </td>";
}
}
echo "</table>";
}
感谢您的时间。