I'm rewriting a URL with hyphens using item_name
column. for instance animals of the planet-v9
to http://example.com/book/animals-of-the-planet-v9
with str_replace(' ','-',$item_name)
. How I'm approaching back to MySQL is:
$item_name = str_replace('-', ' ', $_GET['item_name']);
SELECT * FROM table where item_name LIKE '% $item_name %'
"animals-of-the-planet -v9" Becomes "animals of the planet v9 "
URL: example.com/animals-of-the-planet-v9
The above query matches 0 results, I noticed any of column that contains hyphen - is not matching because sometimes it needs one original hyphen other than the rewritten ones in string. I also tried with = and LIKE in query. How do I figure this out where I can have a pretty simple URL containing hyphens without any numeric ids?
Try this,
$item_name = $_GET['item_name'];
SELECT * FROM table where REPLACE(item_name, ' ', '-') = '$item_name'
Note - Escape $item_name before using it. To prevent your query from mysql injection.