Can anyone find me a solution.
I want to import csv file into mysql database. I have the code to insert.But the rows that contains comma in data field is not getting inserted and is skipped.All the other rows are inserted.
This is the code that i have..
[php]@mysql_close($con);
$con = mysql_connect("$hostname","$user","$pass") or die("did'nt connect to mysql");
mysql_select_db("test") or die ("no database");
$databasetable = "$file3";
$lines = 0;
$queries = "";
$linearray = array();
foreach(split($lineseparator,$csvcontent) as $line) {
$count = $count + 1;
$lines++;
$line = trim($line," \t");
$expr="/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/";
$results=preg_split($expr,trim($line));
$results1= preg_replace("/^\"(.*)\"$/","$1",$results);
$linearray = explode($fieldseparator,$results1);
$linemysql = implode("','",$linearray);
if($addauto)
{
$query = "insert into $databasetable VALUES('$count','$linemysql');";
$result1 = mysql_query($query) or die("didn't query for insert");
}
else
$query = "insert into $databasetable values('$linemysql');";
$queries .= $query . "\n";
@mysql_query($query);
}
echo " CSV File imported to the database";
@mysql_close($con);
[/php]
It is inserting all the rows except the row that has comma in it.I couldn rectify it.Can anyone help me out please.. thank u.
3 2661
Welcome to The Scripts!
Please enclose your posted code in [code] tags (See How to Ask a Question).
This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.
Please use [code] tags in future.
MODERATOR
Your code if very difficult to follow, without any comments. And whatever happened to the good old (standard PHP) fgetcsv() function?
To get some insight into the code, what I miss is what is the content of the reg.ex in the splits, i.e. $lineseparator and $fieldseparator.
Since the record is not inserted into the db, it would be handy to know how the sql statement looks like, so, for debug reasons, add an echo to each of the SQL statements before you execute it.
(The last mysql_query is not even tested for errors, so how do you know what happens?)
Ronald
This is the full coding -
$path = $fileloc;
-
$file = basename($path); // $file is set to "index.php"
-
-
$databasehost = "localhost";
-
$databasename = "test";
-
-
$databaseusername ="test";
-
$databasepassword = "";
-
$fieldseparator = ",";
-
$lineseparator = "\n";
-
$csvfile = $fileloc;
-
-
-
$addauto =1;
-
-
if(!file_exists($csvfile)) {
-
echo "File not found. Make sure you specified the correct path.\n";
-
exit;
-
}
-
-
-
$file = fopen($csvfile,"r");
-
-
if(!$file) {
-
echo "Error opening data file.\n";
-
exit;
-
}
-
-
$size = filesize($csvfile);
-
-
if(!$size) {
-
echo "File is empty.\n";
-
exit;
-
}
-
-
$csvcontent = fread($file,$size);
-
-
fclose($file);
-
-
$con = mysql_connect("$hostname","$user","$pass") or die("did'nt connect to mysql");
-
mysql_select_db("test") or die ("no database");
-
$result = mysql_query("SELECT * FROM ex order by count");
-
$count=0;
-
while($row = mysql_fetch_array($result))
-
{
-
$count= $row['count'];
-
}
-
if($count==0)
-
$count=0;
-
else
-
$count=$count;
-
-
-
@mysql_close($con);
-
$con = mysql_connect("$hostname","$user","$pass") or die("did'nt connect to mysql");
-
mysql_select_db("test") or die ("no database");
-
-
$databasetable = "$file3";
-
$lines = 0;
-
$queries = "";
-
$linearray = array();
-
-
foreach(split($lineseparator,$csvcontent) as $line) {
-
$count = $count + 1;
-
-
$lines++;
-
-
$line = trim($line," \t");
-
-
$expr="/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/";
-
-
$results=preg_split($expr,trim($line));
-
-
$results1= preg_replace("/^\"(.*)\"$/","$1",$results);
-
-
$linearray = explode($fieldseparator,$results1);
-
-
$linemysql = implode("','",$linearray);
-
-
if($addauto)
-
{
-
$query = "insert into $databasetable VALUES('$count','$linemysql');";
-
$result1 = mysql_query($query) or die("didn't query for insert");
-
}
-
else
-
$query = "insert into $databasetable values('$linemysql');";
-
$queries .= $query . "\n";
-
@mysql_query($query);
-
}
-
@mysql_close($con);
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: David Berry |
last post by:
Hi All. I'm looking for any help or sample code that can show me how to make a file import wizard in ASP.NET (VB preferred) like the one that MS Access uses. I'm working on a web site where the...
|
by: DCM Fan |
last post by:
Access 2K, SP3 on Windows 2K, SP4
All,
I have an import spec set up with quoted Identifiers and comma-separated
values. The text file is produced by a 3rd-party program of which I have no...
|
by: Steemer |
last post by:
Okay, I checked with usual suspects and I'm still frustrated.
..txt file, fixed width. About 88 fields of varying length. All text
fields. It will only let me add seperators to about the 410th...
|
by: orange |
last post by:
I've got a database in .txt file similar to this:
BookName;Author;Year;ReviewedBy;Rating;Pages
Nemesis;Isaac Asimov;1989;13,31,24;good;110
Sense & Sensibility;Jane Austen;1970;45,32;great;120...
|
by: holdemfoldem |
last post by:
Hi. I'm new to this board and have a few questions about using the
method referred to in the topic of this message.
I have manually transferred over 1/2 million records from a text file
into my...
|
by: ninrulz |
last post by:
I will try to explain my situation. I know that it is hard to offers
solutions without fully understanding what people would like to
achieve.
I receive 2 csv files every month. The csv files...
|
by: Someone |
last post by:
Hello
Using the c file io functions if reading a csv file there is a problem if a
field contains an embedded carriage return. So I can check for apostrophe
pairs but processing does seem a bit...
|
by: cabrenner |
last post by:
I am new to SQL Server, and migrating part of an Access application to
SSE. I am trying to insert a comma delimited file into SSE 2005. I am
able to run a BULK INSERT statement on a simple file,...
|
by: GarryJones |
last post by:
(I am sorry if this is the wrong group for this posting, I cant find a
group on usenet for phpmyadmin, but maybe someone would be nice enough
to answer me anyway....)
I need to import data into...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |