It's been a while since I have done any coding and have a url that looks like: /products/some-productname Theres a rewite rule that reads that last folder as a get variable like $_GET['product'] = "value" I want to query the DB on that value, but when it has a dash in it, it does not function like I want it to, although I want to make it readable by having dashes in there. Whats the best way to handle so that I can get the WHERE productSlug = "$product"?
WHERE productSlug = str_replace("-"," ",$product); // try single quotes if this doesn't work, remember to leave a space to replace - with an empty space PHP: Failing the above, alter $product before the query: $product = str_replace("-"," ",$product); // there's no effect unless the product name has a dash WHERE productSlug = "$product"; PHP: Example test.php <?php $product = "some-productname"; echo $product; // some-productname echo "<br / >"; $product = str_replace("-"," ",$product); echo $product; // some productname ?> PHP: OUT_PUT: some-productname some productname Code (markup):
Thanks guys. I just stripped out the spaces and dashes, but I wanted to have it look more readable than that, but just cant get it to work with my query. Im good.
Are you quoting the string? ('-myproduct', not myproduct, within the quoted string? IOW, it should look like "blah blah where foo = '-bar';") You could also try "where foo like '-bar%';" You might also try mysql_real_escape_string(). What will work depends more on the database than on anything else.