Hi guys! Need help if 2 words have a product - it is in 2 categories! In the database it looks like this - the first column is the product ID - 2 category IDs where it is attached!

enter image description here

So I write the code:

$dad_result = mysql_query("select * from oc_product_to_category where (product_id = 99)") or die(mysql_error()); $oc_product_to_category = mysql_fetch_array($dad_result); echo $oc_product_to_category['category_id']; 

and displays only the first value, but the second one is needed!

Tell me what I'm doing wrong!

  • it’s not very clear what the second meaning is, if you don’t order the answer in any way - splash58
  • in the database there is no "first (or second) value", in the database there is a "first (or second, third, etc.) value in the resulting set", i.e. By performing the same query twice, you can get a completely different data set and, as a result, the second value will be different each time. Therefore, it is necessary to know how to distinguish the "first" from the "second". Most often, to specify a specific order, sorting by a specific field is used, then (with rare exceptions) it is already possible to get the expected result for each request (that is, the same data for several identical requests) - BOPOH
  • @BOPOH yes, you are not lazy, sir :) I only had enough for 10 words :))) - splash58
  • thanks guys i will try! - lxxnutsxxl
  • @ splash58 Well, this is not the answer) - BOPOH

1 answer 1

The program, worked as you requested, brought the value. If you used a loop, you would get all the values:

 while ($oc_product_to_category = mysql_fetch_array($dad_result)) { echo $oc_product_to_category['category_id']; } 

You can also use mysql_result to get, specifically, the 2nd value:

 echo mysql_result($oc_product_to_category, 1, 1); 
  • and how to get exactly the second value? - lxxnutsxxl
  • @lxxnutsxxl corrected - LamerXaKer