-1

I have a column in mysql table named category.

Clothese category is 1
Mens clothes is 1-1
Ladies clothes is 1-2
Kids clothes is 1-3

The goal for above structure is to easily load all clothes in one page with below query.

$cat=mysql_real_escape_string($_GET[cat]); 
$query="select * from campaigns WHERE category LIKE '$cat%' ";

Till here it works fine but the problem is I have another category with ID 10 for perfumes, When I want to load category 1 (including 1 itself and it's sub-categories like 1-1 ) it's loading category 10 as well.

Please let me know what is the proper solution

Strawberry
  • 33,750
  • 13
  • 40
  • 57
  • 1
    can "select * from campaigns WHERE category LIKE '{$cat}-%' " solve your problem for perfumes ` LIKE '10-%'` – Ahed Eid Jul 10 '16 at 17:11
  • 3
    Save yourself some time and rewrite the application to not use the `mysql_*` family of functions: http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Sumurai8 Jul 10 '16 at 17:11
  • I applied your code and both URLs below are not loading any product products.php?cat=10 products.php?cat=10- – Hamid Joukar Jul 10 '16 at 17:39

1 Answers1

1

query can be for:1-1,1-2

$query="select * from campaigns WHERE category LIKE '$cat-%' ";

Same will work for 10 also Edit:

$query1=mysql_numrows("select * from campaigns WHERE category LIKE '$cat-%' ");
if($query1>0) $str="-"
else $str="";
$query_final=mysql_query("select * from campaigns WHERE category LIKE '$cat$str%'");
SattyamBhatt
  • 338
  • 2
  • 13