views:

24

answers:

2
+1  Q: 

MySQL Query Help

I need to find the category id in the products table below. However the cms_ecom_categories.id is wrapped with the & character like &12&. Is there some kind of wild card i could use to wrap around? like %cms_ecom_categories.id% ?

 $sql = "SELECT * FROM cms_ecom_products, cms_ecom_categories 
         WHERE cms_ecom_products.pCategories = cms_ecom_categories.id
               AND cms_ecom_categories.slug = ".$page."";

Thanks to all in advance who can help.

+1  A: 

You can use LIKE => WHERE id LIKE %12%

Or add the &-signs to the id: $page = '&' . $page . '&' => WHERE id = $page

Alec
Thank you, i'll give it a whirl
Andy
So the syntax should be something like:$sql = "SELECT * FROM cms_ecom_products, cms_ecom_categories WHERE cms_ecom_categories.id LIKE %cms_ecom_products.pCategories% AND cms_ecom_categories.slug = ".$page."";
Andy
Sorry, thought it was about the $page variable. Try this: `LIKE CONCAT('%', cms_ecom_products.pCategories, '%')`; that should make it work.
Alec
+1  A: 
  • Either use AND cms_ecom_categories.slug = '&".$page."&' - just put the ampersands in the quotes
  • Or use the _ wildcard, meaning exactly one character: AND cms_ecom_categories.slug LIKE '_".$page."_'
  • Or use the % wildcard, meaning zero or more characters: AND cms_ecom_categories.slug LIKE '%".$page."%'

It might be better to modify the $page variable itself though. And why are you building SQL-queries from a string? You should check parameterized queries or at least escape, it's easier and more secure.

Konerak
Thanks Konerak, my actual full statement consists of (which i believe is secure) $sql = "SELECT * FROM cms_ecom_products, cms_ecom_categories WHERE cms_ecom_categories.id LIKE %cms_ecom_products.pCategories% AND cms_ecom_categories.slug = ".$page.""; $stmt = Record::$__CONN__->prepare( $sql ); $stmt->execute();
Andy
It depends if you escape the page variable before or not. You might want to put `AND cms_ecom_categories.slug = ?` and later do `$stmt->execute($page)` :)
Konerak
Thanks for your help.
Andy
Andy