simpjd Posted September 20, 2008 Share Posted September 20, 2008 Hello, I'm trying to do a mysql select statement but incorporate something like a preg_replace or trim into the search! So, for instance: $sql = "SELECT * FROM products where category=$_GET['category']"; could be something like: $sql = "SELECT * FROM products where preg_replace('/[^a-zA-Z0-9s]/', '',category)=$_GET['category']"; But obviously this doesn’t work! So, for example, If $_GET="tests123ab" I want the search to pick up "TeSt's 123 aB" (I want to strip whitespace and symbols from the table entities during the search!" If someone could help me with this, I would be greatly appreciative! Jack Quote Link to comment https://forums.phpfreaks.com/topic/125123-solved-filter-select-search-result/ Share on other sites More sharing options...
php.ajax.coder Posted September 20, 2008 Share Posted September 20, 2008 Try <?PHP $category = $_GET['category']; $sql = "SELECT * FROM products where category= '$category' "; ?> Quote Link to comment https://forums.phpfreaks.com/topic/125123-solved-filter-select-search-result/#findComment-646729 Share on other sites More sharing options...
.josh Posted September 21, 2008 Share Posted September 21, 2008 Looks like you want to make use of sql's LIKE SELECT column FROM table WHERE column LIKE 'something' You can use some regex withing your 'something' to filter results. For instance, if you have a column like so: Names John Joe Jane Joanna Mary and you ran this query: SELECT names FROM table WHERE names LIKE 'Jo%' John, Joe and Joanna would be returned. You will pretty much have to build your '...' string the way you want it to be returned, just like with any regex operation. So for instance, if you want "tests123ab" to return "TeSt's 123 aB" you would have to add %'s like so: "%TeSt%s%123%aB%" That's something you're going to have to build with php and there's really no set method of doing it; it just depends on how "open ended" you want your search to be. In php you could easily explode your $_GET var and add a % between every character and it would return that data just the same, but I don't really think that's all that practical... Quote Link to comment https://forums.phpfreaks.com/topic/125123-solved-filter-select-search-result/#findComment-646774 Share on other sites More sharing options...
simpjd Posted September 21, 2008 Author Share Posted September 21, 2008 Oh ok, using % as a wild card would work! Thanks for the advice! Quote Link to comment https://forums.phpfreaks.com/topic/125123-solved-filter-select-search-result/#findComment-646934 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.