Forum Moderators: coopster

Message Too Old, No Replies

Database searching

Finding data thats NOT in the database

         

James_V

6:58 am on Jun 29, 2005 (gmt 0)

10+ Year Member



Im looking for a way to compair a product database with a list of numbers to return the numbers not in the database.
The list is from a text file with the format of:
(Product, Qty)
1234, 1
5678, 1
56963, 0
So what I need to do is find what products in the list are not in the database and echo them back.

Kind of like this:

$sql = mysql_query("SELECT products_model FROM products") or die("Invalid query: " . mysql_error());
$result = mysql_query($sql);
$handle = fopen("http://www.123456789.com/inventory.txt", "r");
while(!feof( $handle ) )
{
$file =fgets( $handle );
$values = explode("\n", $file);
$resp = in_array('$values[0]', $result););
if ($resp) {//do nothing }
else {
echo "$values[0]<br>";
}
}
fclose( $handle );

I know this is way wrong, just need a good push in the right direction.

mcibor

8:54 am on Jun 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What I would do is create an array of products from the file to achieve this form:
$in_file = array("1234", "5678", "56963");

then do such trick:
$sql = "SELECT products_model FROM products WHERE products_model IN ('" . implode(", ", $in_file) ."')";//this will give results that are in db
//get the results into an array
//$result = array("1234");//that's in db

$difference = array_diff($in_file, $result);
//$difference is 5678, 56963

Hope this sets you on right track. The only problem may be getting the in_file array.

Best regards
Michal Cibor

James_V

10:57 pm on Jun 29, 2005 (gmt 0)

10+ Year Member



Thanks for the help! Got me thinking and here is what I came up with. Its not pretty but works.

$stack = array("Start");
$data = array ("Start");
$handle = fopen("http://www.123456789.net/inventory.txt", "r");
while(!feof( $handle ) )
{
$file =fgets( $handle );
$values = explode(",", $file);
array_push($stack, $values[0]);
}
$sql = "SELECT products_model FROM products";
$query = mysql_query($sql);
while ($result = mysql_fetch_array($query)) {
array_push($data, $result[0]);
}
$difference = array_diff($stack, $data);
while (list(, $value) = each($difference)) {
echo "New Item: $value<br />\n";
}
fclose( $handle );
mysql_close($link);

Thanks again for all the help!