Hi, I am reading a csv file and i read it line by line.. but there is a problem.

It reads the csv line but put the spaces between each charachter.. and the line gives problem when i use it write in the mysql table..

Here is the out put of the array...

temp1[0]: ÿ
temp1[1]: þ
temp1[2]: c
temp1[3]: 
temp1[4]: h
temp1[5]: 
temp1[6]: e
temp1[7]: 
temp1[8]: c
temp1[9]: 
temp1[10]: k
temp1[11]: 
temp1[12]:  
temp1[13]: 
temp1[14]: i
temp1[15]: 
temp1[16]: t
temp1[17]: 
temp1[18]:  
temp1[19]: 
temp1[20]: o
temp1[21]: 
temp1[22]: u
temp1[23]: 
temp1[24]: t

Here is the code i am using to read the csv.. i can post the csv also if any one needs it...

$readcsv = fopen('csvdata/1747.csv', 'r');
$row = 0; 
$r = 0;
$activerow = 0 ;


  while ($line = fgetcsv($readcsv,1024)) {
     $data[$row] = $line[0];
	// echo "Data is ".$data[$row];
	$row++;
	}
	$temp1 = $data[0];
	/*The While loop is there to just check the spaces, it is not the part of program*/
	while ( $r < 50){
	echo "\ntemp1[".$r."]: ".$temp1[$r];
	$r++;
	}
	
$sql = "CREATE TABLE $temp1 (dish_id INT,dish_name VARCHAR(255),price DECIMAL(4,2),is_spicy INT)"; echo "\n\n\n\n".$sql;
	mysql_query($sql) or die("\nCannot query ".mysql_error());

Recommended Answers

All 3 Replies

Please post the .csv. As an attachment, not a copy-paste please.

Please post the .csv. As an attachment, not a copy-paste please.

The file is uploaded in the form of zip file please unzip the file to get the csv.

place a copy the csv file on your desktop, and rename the file 1747.txt, the open it in Notepad or Wordpad, and you'll see that your script is doing exactly as it should. The file is full of blank lines and multiple spaces between letters, with tabs separating the fields.

Long story short, the file is pooched.

You'll need to run the file out again from its original source (Excel, etc), and see if it comes out the same.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.