Jump to content

Second Loop Issue


e1seix

Recommended Posts

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  8)

Link to comment
Share on other sites

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.