HDFilmMaker2112 Posted May 14, 2011 Share Posted May 14, 2011 I'm looking to know if MySQL or PHP would cause problems if a database entry has a space in the beginning of it. Say I have a two entries in a DB. "Test" and " Test" *space before T. If one of my users does a search for "Test" would it also return the result of " Test"? Quote Link to comment https://forums.phpfreaks.com/topic/236392-spaces-in-field/ Share on other sites More sharing options...
Zane Posted May 14, 2011 Share Posted May 14, 2011 It depends on your search script/query.. but typically No it wouldn't turn them both up. The query would need to use a LIKE clause with wildcards on either end SELECT * FROM table WHERE field LIKE '%Test%' Quote Link to comment https://forums.phpfreaks.com/topic/236392-spaces-in-field/#findComment-1215319 Share on other sites More sharing options...
fugix Posted May 15, 2011 Share Posted May 15, 2011 the text that the user types in would have to include the whitespace in order for the query to return "Test" without the whitespace. The two strings must be the same unless you use the method that Maq posted Quote Link to comment https://forums.phpfreaks.com/topic/236392-spaces-in-field/#findComment-1215549 Share on other sites More sharing options...
fenway Posted May 16, 2011 Share Posted May 16, 2011 But of course you're not searching an entire DB this way.... Quote Link to comment https://forums.phpfreaks.com/topic/236392-spaces-in-field/#findComment-1215860 Share on other sites More sharing options...
Pikachu2000 Posted May 16, 2011 Share Posted May 16, 2011 If you're only concerned with leading/trailing spaces, you can use MySQL's TRIM() function on the data in the table, and php's trim() function on the data you're comparing. $data = trim($_POST['data']); $query = "SELECT field1, field2 FROM table WHERE TRIM(field) = '$data'"; Quote Link to comment https://forums.phpfreaks.com/topic/236392-spaces-in-field/#findComment-1215891 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.