473,322 Members | 1,493 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,322 software developers and data experts.

php array routine, help please

Hello All,

I am reading a csv-file into an array and inserting it into a mysql
database.
The first 2 fields in the table are NOT NULL and required, and the
last field in the table may be empty, but in the csv-file there is no
indication of this, such as an empty "".

For example, the format of the csv-file is:
===================================
"value1", "value2"
"value1", "value2", "value3"
"value1", "value2", "value3"
"value1", "value2"
"value1", "value2"
"value1", "value2", "value3"
===================================

Value1 and Value2 will always be on each line, however value 3 may or
may not.

The following code works fine until it finds a line in the csv file
that only has the first 2 required values...
The error is obviously "Column count doesn't match value count at row
1"

How could I:
fix this routine to find out if each line only has 2 entries
and how to have my $sql reflect that?

BTW, the '' in the sql statement is for the first value field, which is
auto-increment...

thanks!

================================================== ===============================

$fcontents = file ('./test_file.txt');
for ($i=0; $i<sizeof($fcontents); $i++) {
$line = trim($fcontents[$i]);
$arr = explode("\"", $line);

$sql = "insert into log_data values ('',". implode("'", $arr).")";

if(mysql_error()) {
echo "<b>" . mysql_error() ."</b><br><br>\n";
}
}
================================================== ===============================

Jul 17 '05 #1
1 1273


john wrote:
Hello All,

I am reading a csv-file into an array and inserting it into a mysql
database.
The first 2 fields in the table are NOT NULL and required, and the
last field in the table may be empty, but in the csv-file there is no
indication of this, such as an empty "".

For example, the format of the csv-file is:
===================================
"value1", "value2"
"value1", "value2", "value3"
"value1", "value2", "value3"
"value1", "value2"
"value1", "value2"
"value1", "value2", "value3"
===================================

Value1 and Value2 will always be on each line, however value 3 may or
may not.

The following code works fine until it finds a line in the csv file
that only has the first 2 required values...
The error is obviously "Column count doesn't match value count at row
1"

How could I:
fix this routine to find out if each line only has 2 entries
and how to have my $sql reflect that?

BTW, the '' in the sql statement is for the first value field, which is
auto-increment...


First, I would re-think how to solve the problem.

Have you looked at the function fgetcsv()? This function will read csv
files into arrays. That way for each line read in you can tell how many
value you have by looking at the count of the generated array.

As for your second question, instead of using the insert format you
have used, I would use

insert into xyx set field1='value1', field2='value2'

That way you can just set the fields you have. For example:

<?
$fields = array('field1','field2','field3','field4');
$fp = fopen('yourcsv.file','r');
while (($tmp = fgetcsv($fp)) != FALSE) {
$q = "insert into xyz set ";
$tmpq = array();
for ($i=0;$i<count($tmp);$i++) // $tmp holds the results from
fgetcsv()
$tmpq[] = $fields[$i] . "='" . $tmp[$i] . "'";
$q .= implode(',',$tmpq);
echo $q."<br>\n"; //debugging only
$rs = mysql_query($q) or die('Problems ... ' . mysql_error());
}
fclose($fp);
?>

Code not checked for errors.

Ken

Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
2
by: Tim Pascoe | last post by:
I'm trying to implement a sorting routine for a string array. The sort routine is from http://ourworld.compuserve.com/homepages/attac-cg/acgsoft.htm (a great selection of routines in the 'Sequence...
2
by: Newsgroup Posting ID | last post by:
i'm new to c and have gotten my program to run but by passing hardcoded array sizes through the routines, i want to make the array extendable by using realloc but i'd be doing it two routines down,...
7
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort...
6
by: Neo | last post by:
Dear All, I want to know how a subroutine should return an array of values to the main program. From the main program, I call a sub-routine 'get_sql' which then fetches data from oracle db using...
7
by: Morgan | last post by:
I have read much posts on the argument but no one clearly says if this operation is possible or not. Simply I have a routine which reads from a text file some integer arrays (1 or 2D). The...
15
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
8
by: luke.yolanda | last post by:
is anyone know how to implement a huge 2-D array? such as ' unsigned char a; ' I compile and run it in VC 6.0, but it show me an error.... thank you guys
27
by: mike3 | last post by:
Hi. I can't believe I may have to use an array here. I've got this bignum package I was making in C++ for a fractal generator, and tried an approach that was suggested to me here a while...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.