PHP/Development/CSV
Содержание
explode CSV file
<source lang="html4strict">
<?php
$users = file("./demoCSV.csv"); foreach ($users as $user) { list($name, $email, $phone) = explode(",", $user);echo "
$name ($email) Tel. $phone
";}
?>
</source>
fgetcsv: read a csv file
<source lang="html4strict">
<?php
$fh = fopen("./demoCSV.csv", "r"); while (list($name, $email, $phone) = fgetcsv($fh, 1024, ",")) {echo "
$name ($email) Tel. $phone
";}
?>
</source>
Loading table data from a CSV file
<source lang="html4strict">
<?php
$conn = mysql_connect("localhost","someuser","secret"); $db = mysql_select_db("cars"); $fh = fopen("yourCSVFile.csv", "r"); while ($line = fgetcsv($fh, 1000, ",")) { $year = $line[0]; $make = $line[1]; $color = $line[2]; $query = "INSERT INTO carcollection SET year="$year", make="$make",color="$color""; $result = mysql_query($query); } fclose($fh); mysql_close();
?>
</source>
Read csv file
<source lang="html4strict">
<?php
$file = "./demoCSV.csv"; $fh = fopen($file, "rt"); $userdata = fread($fh, filesize($file)); fclose($fh); echo $userdata;
?>
</source>