$fp = fopen('csv文件名', 'w');
$rs = mysql_query('select * from tbl_name');
while($row = mysql_fetch_assoc($rs) {
fputcsv($fp, $row);
}
fclose($fp);
$rs = mysql_query('select * from tbl_name');
while($row = mysql_fetch_assoc($rs) {
fputcsv($fp, $row);
}
fclose($fp);
解决方案 »
- 求一个iis下的rewrite规则
- 怎么删除TSV文件每一行中,""引号里面的换行符
- HTML Parsing Error: Unable to modify the parent container element before the chi
- php对oracle的一个操作类,部分没看懂
- joomla 1.5 Select menu item type 那里没有自己的组件,怎么弄出来
- 验证码问题
- 求解三维数据变二维数组
- WordPress搬家方法及乱码的解决方法
- 关于在模板判断问题,请教?
- Class 'ZipArchive' not found
- ajax能返回,但是不刷新当前页面。要强制刷新才管用
- Thinkphp中volist很诡异的一个现象,求解答
如果你有权限访问数据库目录,也可以建一个 ENGINE=CSV 的表,复制数据进去
<form enctype="multipart/form-data" action="import.php" method="POST">
导入模板
<label for="文件选择">文件选择:</label><input name="csv_goods" type="file" />
<input type="submit" value="导入" name="import" />
</form>
<?php
if (isset($_POST['import'])){
$file = $_FILES['csv_goods'];
$file_type = substr(strstr($file['name'],'.'),1);
// 检查文件格式
if ($file_type != 'csv'){
echo '文件格式不对,请重新上传!';
exit;
}
$handle = fopen($file['tmp_name'],"r");
$file_encoding = mb_detect_encoding($handle);
// 检查文件编码
if ($file_encoding != 'ASCII'){
echo '文件编码错误,请重新上传!';
exit;
}
$row = 0;
while ($data = fgetcsv($handle,1000,',')){
//echo "<font color=red>$row</font>"; //可以知道总共有多少行
$row++;
if ($row == 1)
continue;
$num = count($data);
// 这里会依次输出每行当中每个单元格的数据
for ($i=0; $i<$num; $i++){
echo $data[$i]."<br>";
// 在这里对数据进行处理
}
}
fclose($handle);
}?>