Jump to content

Recommended Posts

I made a couple of changes to a script that reads new data from a text file and updates mysql based on a field matching the text file.

That field is $data_row[0] in the text file and products_sku in the following code.

	while (($data_row = $reader->getNextLine()) != NULL) {	

		$products_sku = tep_db_prepare_input($data_row[0]);
		$products_model = $data_row[1];
//original	$products_name = ucwords(strtolower($data_row[2]));
		$products_name = rtrim(ucwords(strtolower($data_row[8]))) . ' ' . rtrim(ucwords(strtolower($data_row[2]))) . ' ' . $data_row[1];
		$products_width = $data_row[3];
//note there's a bunch more fields and the rest of the query
// but this is the important part

from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_sku = '" . $products_sku . "' and p.products_id = pd.products_id and pd.language_id = '1'");

What's happening is products that don't have a matching products_sku are being written over with all zeros in certain fields. The other part that I changed just before this problem appeared is this;

class csv_reader {
var $fp;
	function csv_reader($filename = '') {
		$this->fp = -1;
		if ($filename != '') {
			if (file_exists($filename)) {
				$this->fp = fopen($filename, "r");
			}
		}
	}
	function getNextLine() {
		if ($this->fp == -1) return NULL;
		if (!feof($this->fp)) {
			$line = fgets($this->fp);
		        return explode("~", $line);
//original		$arr = fgetcsv($this->fp, 0, "~"); 
//original		return $arr;
		}
		else {
			fclose($this->fp);
			return NULL;
		}
	}
}

I changed from fgetcsv after reading http://php.net/manual/en/function.fgetcsv.php  - post by jaimthorn at yahoo dot com  I was having the same problem he described.

 

Any thoughts on whether my code changes might be causing some fields to be written in all zeros where the products_sku is blank (not NULL, just blank).

Link to comment
https://forums.phpfreaks.com/topic/275332-help-with-text-file-reader/
Share on other sites

You don't show the full query that you are running. Also, is that the ONLY query you are running in this process?

 

Also, this line:

 

$products_name = rtrim(ucwords(strtolower($data_row[8]))) . ' ' . rtrim(ucwords(strtolower($data_row[2]))) . ' ' . $data_row[1];

 

Can be simplified to

 

$products_name = ucwords(strtolower( rtrim($data_row[8]).' '.rtrim($data_row[2]) )) . $data_row[1];

 

It could probably be simplified even more, but without seeing the possible input data I can't be sure

Thanks for the reply & the code.

It is the only query that uses this particular text file and this db table. And the only one producing errors in the data.

 

The csv_reader is shared by a number of scripts, some of those were the ones getting goofed up because of un-escaped double quotes in the text.

I can't figure out how or exactly where fgets fails where fgetcsv had no problem, but as a workaround I'm going to return to fgetcsv for this script only and try that.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.