Comparing Table column data with other acquired data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Comparing Table column data with other acquired data

Post 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 ;)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
Post Reply