PHP/Development/CSV
Содержание
explode CSV file
<?php
$users = file("./demoCSV.csv");
foreach ($users as $user) {
list($name, $email, $phone) = explode(",", $user);
echo "<p>$name ($email) Tel. $phone</p>";
}
?>
<!--
A,a@example.ru,11111
B,b@example.ru,22222
-->
fgetcsv: read a csv file
<?php
$fh = fopen("./demoCSV.csv", "r");
while (list($name, $email, $phone) = fgetcsv($fh, 1024, ",")) {
echo "<p>$name ($email) Tel. $phone</p>";
}
?>
<!--
A,a@example.ru,11111
B,b@example.ru,22222
-->
Loading table data from a CSV file
<?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();
?>
Read csv file
<?php
$file = "./demoCSV.csv";
$fh = fopen($file, "rt");
$userdata = fread($fh, filesize($file));
fclose($fh);
echo $userdata;
?>