473,385 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

csv import which contains comma in data field

2
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.
Mar 25 '08 #1
3 2661
ronverdonk
4,258 Expert 4TB
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
Mar 25 '08 #2
ronverdonk
4,258 Expert 4TB
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
Mar 25 '08 #3
SMusic
2
This is the full coding
Expand|Select|Wrap|Line Numbers
  1. $path = $fileloc;
  2. $file = basename($path);         // $file is set to "index.php"
  3.  
  4. $databasehost = "localhost";
  5. $databasename = "test";
  6.  
  7. $databaseusername ="test";
  8. $databasepassword = "";
  9. $fieldseparator = ",";
  10. $lineseparator = "\n";
  11. $csvfile = $fileloc;
  12.  
  13.  
  14. $addauto =1;
  15.  
  16. if(!file_exists($csvfile)) {
  17.     echo "File not found. Make sure you specified the correct path.\n";
  18.     exit;
  19. }
  20.  
  21.  
  22. $file = fopen($csvfile,"r");
  23.  
  24. if(!$file) {
  25.     echo "Error opening data file.\n";
  26.     exit;
  27. }
  28.  
  29. $size = filesize($csvfile);
  30.  
  31. if(!$size) {
  32.     echo "File is empty.\n";
  33.     exit;
  34. }
  35.  
  36. $csvcontent = fread($file,$size);
  37.  
  38. fclose($file);
  39.  
  40. $con = mysql_connect("$hostname","$user","$pass") or die("did'nt connect to mysql");
  41.  mysql_select_db("test") or die ("no database");
  42. $result = mysql_query("SELECT * FROM ex order by count");
  43. $count=0;
  44. while($row = mysql_fetch_array($result))
  45.   {
  46.    $count= $row['count'];
  47.   }
  48. if($count==0)
  49.   $count=0;
  50.   else
  51.   $count=$count;
  52.  
  53.  
  54.  @mysql_close($con);
  55.  $con = mysql_connect("$hostname","$user","$pass") or die("did'nt connect to mysql");
  56.  mysql_select_db("test") or die ("no database"); 
  57.  
  58. $databasetable = "$file3";
  59. $lines = 0;
  60. $queries = "";
  61. $linearray = array();
  62.  
  63. foreach(split($lineseparator,$csvcontent) as $line) {
  64. $count = $count + 1;
  65.  
  66. $lines++;
  67.  
  68. $line = trim($line," \t");
  69.  
  70. $expr="/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/";
  71.  
  72. $results=preg_split($expr,trim($line));
  73.  
  74. $results1= preg_replace("/^\"(.*)\"$/","$1",$results);
  75.  
  76. $linearray = explode($fieldseparator,$results1);
  77.  
  78. $linemysql = implode("','",$linearray);
  79.  
  80. if($addauto)
  81. {
  82. $query = "insert into $databasetable VALUES('$count','$linemysql');";
  83. $result1 = mysql_query($query) or die("didn't query for insert");
  84. }
  85. else
  86. $query = "insert into $databasetable values('$linemysql');";
  87. $queries .= $query . "\n";
  88. @mysql_query($query);
  89. }
  90. @mysql_close($con);
  91.  
Mar 27 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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...
1
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...
1
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...
4
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...
3
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...
3
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...
11
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...
9
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,...
2
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...
0
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,...
0
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$) { } ...
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.