Page 1 of 1

Comparing Table column data with other acquired data

Posted: Mon Jan 15, 2007 3:21 pm
by MrPotatoes
i wanted to do a brute force check.

i have a script where i get some data from a tab delimited file. the only thing that i'll check is the first part or $data[0]. from there i check to see if that is the same as anything else within the MySQL tables. so i wanted it to cycle though the whole tables' columns and check to see if it's there. if not then it'll add it, otherwise it skips it.

this is the code that i have thus far:

Code: Select all

// set file to read
$FileRead = "token.test.txt";
// open file
$FileReadHandle  = fopen ($FileRead, "r") or die("Could not open file");

$i = 0;
$file = null;

while (!feof($FileReadHandle)) 
{
	$data = fgets($FileReadHandle);
	$data = explode("\t", $data);
	$margin = @(($data[1] - $data[2]) / $data[1]);

	// this means that we entered a row in the file that has no data.  
	// skip it and continue.  most likely we are at the end anyways
	if ($data[0] == '')
		continue;
	
	$qryTxt = "SELECT part FROM prices WHERE part = '$data[0]' ORDER BY part"
	$result = mysql_query($qryTxt);
	
	while($row = mysql_fetch_array($result))
	{
		if ($row['part'] == $data[0])
		{
			echo 'same: ' . $row['part'] . ' - ' . $data[0] . '<BR>';
		}
		else
		{
			mysql_query("INSERT INTO prices (part) VALUES ('$data[0]')");
		}
			
		$i++;
	}
}

// close file
fclose($FileReadHandle);
so, any good ideas as how to do this?

ps: nevermind syntaxual errors if you come across them. those are easy to fix. thank you though ;)

Posted: Mon Jan 15, 2007 3:36 pm
by Christopher
It doesn't seem like you need to loop through the results from your database query because "part = '$data[0]'" should only return one row. Just check the number of rows returned rather than fetching the row and if a row is found then update the price.