0

Update 2013/12/23 In other words, I have two tables within one database.

If a category exists in one table (e.g.: 43 in featured table) and also exists in another table (43 in category table) then match category ID relevant to the name and if true do something (e.g.: echo).

As far as I can tell nesting an if statement within an if statement that perform two separate MySQL queries doesn't allow me to keep the values of the parent if statement.

I need to somehow store the parent values from the MySQL value so they can be retrieved within the child if statement.

------------------------------

If the category ID in my category_description table exists in another table called sales_promotion then I want to execute another PHP statement.

After doing some research my code so far looks like this

$con = mysql_connect(DB_HOSTNAME,DB_USERNAME,DB_PASSWORD);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

foreach ($breadcrumbs as $breadcrumb) {
$db_selected = mysql_select_db(DB_USERNAME,$con);
$sql = "SELECT * FROM `sales_promotion` LIMIT 0, 30";
$result = mysql_query($sql,$con);

$sql_categories = "SELECT * FROM `category_description` LIMIT 0, 30";
$result_categories = mysql_query($sql_categories,$con);

$test = mysql_fetch_array($result);
  while($row = mysql_fetch_array($result_categories))
  {
    if (strpos($row['name'],$breadcrumb['text']) !== false) {
    echo('<p>Category '.$row['name'].' contains '.$row['category_id'].'</p>');
    $category_id = $row['category_id'];

        while($row = mysql_fetch_array($result))
          {
            if (strpos($row['options'],$row['category_id']) !== false) {
            echo('<p>Category '.$row['name'].' contains '.$category_id.'</p>');
            } 
          }


    } 
  }
}

mysql_close($con);

Could someone please explain how I can achieve this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 3
    Wow. There is so much wrong with your code...and I didn't even really look at it because you didn't even explain what your problem with it is. – Till Helge Dec 20 '13 at 16:17
  • I have edited the original post. – user3123345 Dec 23 '13 at 14:21
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – h2ooooooo Dec 23 '13 at 14:41
  • Please don't use mysql_* functions, they're deprecated: http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Marijke Luttekes Dec 23 '13 at 14:42
  • @h2ooooooo forgive me for being lazy, but would you post the raw code to that comment, please? – Marijke Luttekes Dec 23 '13 at 14:43
  • Both of your 'while' statements are using $row. They will not play nicely together this way. – ethrbunny Dec 23 '13 at 14:52

0 Answers0