e1seix Posted December 30, 2009 Share Posted December 30, 2009 Having trouble with this one guys. All help appreciated... <? mysql_connect("", "", "") or die(mysql_error()); mysql_select_db("") or die(mysql_error()); $query_1 = 'SELECT brand_name, COUNT( brand_name ) FROM xml WHERE brand_name != "" GROUP BY brand_name'; $results_1 = mysql_query( $query_1 ) or die(mysql_error()); while($row = mysql_fetch_array( $results_1 )) { $brand_name_1 = $row["brand_name"]; $count_brand_name_1 = $row["COUNT( brand_name )"]; $query_2 = 'SELECT * FROM brands WHERE brand = "'.$brand_name_1.'"'; $results_2 = mysql_query( $query_2 ) or die(mysql_error()); $nums_2 = mysql_num_rows( $results_2 ); while($row = mysql_fetch_array( $results_2 )) { if ($nums_2 == "0") { mysql_query('INSERT INTO brands ( brand, xml, in_stock ) VALUES ( "'.$brand_name.'", "'.$count_brand_name.'", "yes" )') or die(mysql_error()); } } } ?> The brands table is already populated with various "brands". The xml table can change along with the quantity per brand_name. The second loop is supposed to immediately search the brands table for the grouped brand_name. If the "nums" is zero, then it doesn't already exist and therefore the if loop instructs the code to insert the brand_name from the xml table over to brands and populate the count also. There's issues with the second loop. I've ran all sorts of tests without the insert code and even trying to echo and define the brand and it just isn't recognising it. The only thing that seems to register is the $nums_2 which will be "1" if it exists. Can someone either help me correct or suggest a better way of doing this? As I said the second loop is to determine if the grouped brand_name already exists in the brands table under the brand column, if not, then insert it. Many thanks and happy holidays Quote Link to comment https://forums.phpfreaks.com/topic/186647-second-loop-issue/ Share on other sites More sharing options...
fenway Posted December 30, 2009 Share Posted December 30, 2009 Sorry, what are you trying to do? seems lke too many queries. Quote Link to comment https://forums.phpfreaks.com/topic/186647-second-loop-issue/#findComment-985786 Share on other sites More sharing options...
JustLikeIcarus Posted December 31, 2009 Share Posted December 31, 2009 Have you tried something like INSERT INTO brands ( SELECT brand_name, count(1), 'yes' from xml WHERE brand_name NOT IN (SELECT distinct brand from brands) ) Quote Link to comment https://forums.phpfreaks.com/topic/186647-second-loop-issue/#findComment-986280 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.