I want to stock some fields in MySQL coming from Excel but I'm not arrived to cut the lines from Excel in PHP
example:
123,15,0,01/01/2000,456462ABCD,,,
any help please ?
Just use the function for parsing a CSV file
http://php.net/manual/en/function.fgetcsv.php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
Related
I'm trying to read a CSV file that has 800000 line using PHP, but i got this Error:
Allowed memory size of 536870912 bytes exhausted (tried to allocate 4096 bytes)
How could i fix it?
You can read it by chunk.
<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
I'm trying to read this value "<! PC:IDJLOGIN; >" but I get this one IDJLOGIN
whith ths code ,(its the code of the php doc),I have tried with utf-8 and ANSI codification
$row = 1;
if (($handle = fopen($url, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
how can i read ofx(Open financial exchange) with php that is with csv format
<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
?>
I have a CSV file the results of scanning. I just want to read the first column and read the lines up to and including line 6.
This is what I have tried:
<html>
<body>
<h1>MAC Address have been verified</h1>
<table width="50%" border="1">
<tr>
<th>NO</th>
<th>MAC Address</th>
</tr>
<?php
if (($handle = fopen("data.csv", "r")) !== FALSE) {
$row = 1;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
echo "<tr>";
echo "<td><center>".$row++."</center></td>";
echo "<td>".$data[0]."</td>";
echo "</tr>";
} //end while
fclose($handle);
} //end if
?>
</table>
</body>enter code here
</html>
The important line below is
if ( $row > 6 ) break;
<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if ( $row > 6 ) break;
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
?>
I've written a simple bit of code to test two parameters and forward the user to a link.
error_reporting(E_ALL|E_STRICT);
ini_set("display_errors", "On");
$secure_box=$_GET['query'];
$fh = fopen('db.csv', 'r');
$now = date("d.m.Y");
$data=fgetcsv($fh);
$first_name=$data[0];
$date=$data[1];
$url=$data[2];
{
if($secure_box == $first_name AND $date>=$now)
{
header("Location: $url");
exit();
}
else
{
header("Location: http://localhost/x/delivery_method.html");
}
exit;
?>
My problem here is that the entire CSV file is not being read. What can I do ?
The entire file isn't being read because you only called fgetcsv once. fgetcsv only reads one line at a time, you need to put the that code in a loop.
Here is an example of how to use fgetcsv in a loop, from the php docs at http://www.php.net/fgetcsv:
<?php
// Example #1 Read and print the entire contents of a CSV file
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
?>
This is what I've been able to come up with..Hope it's right
<?php
error_reporting(E_ALL|E_STRICT);
ini_set("display_errors", "On");
$name_value = $_GET['query'];
$fh = fopen('db.csv', 'r');
$now = date("d.m.Y");
$line = 1;
if (($handle = fopen("db.csv", "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$num = count($data);
$line++;
for ($c = 0; $c < $num; $c++)
{
if ($name_value == $data[0] AND $data[1] >= $now)
{
header("Location: $data[2]");
exit();
}
else
{
header("Location: http://localhost/x/client_unauthorized.html");
}
}
}
fclose($handle);
}
?>
You can loop to read the whole file line by line until EOF, and check each row in turn.