dnk37482 Posted September 21, 2007 Share Posted September 21, 2007 I have a fields in my database that have spaces in them like "New York" I want them to be able to be called by states.php?id=newyork // with no space Here is what I have so far...it's not working at all though: <?php $name4 = str_replace(" ","", $_REQUEST['id']); $query='SELECT * FROM states WHERE state="'.$name4.'"'; $result= mysql_query($query); $myrow=@mysql_fetch_array($result); if($myrow) { $state=$myrow['state']; } ?> I thank you all in advance for any help I can get.. Quote Link to comment https://forums.phpfreaks.com/topic/70194-string-replace-of-variable-in-database/ Share on other sites More sharing options...
MmmVomit Posted September 21, 2007 Share Posted September 21, 2007 <?php $sql = "SELECT * FROM states WHERE state LIKE '% %';"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/70194-string-replace-of-variable-in-database/#findComment-352569 Share on other sites More sharing options...
dnk37482 Posted September 21, 2007 Author Share Posted September 21, 2007 <?php $sql = "SELECT * FROM states WHERE state LIKE '% %';"; ?> this code doesn't seem to work, but thanks for your help. I am going to try and be more specific. I am trying to replace strings of a variable already in the database, is that even possible? like changing "New York" to "NewYork" so that I can pull states.php?id=newyork instead of states.php?id=new york Quote Link to comment https://forums.phpfreaks.com/topic/70194-string-replace-of-variable-in-database/#findComment-352653 Share on other sites More sharing options...
darkfreaks Posted September 21, 2007 Share Posted September 21, 2007 try using the trim function to trim out whitespace Quote Link to comment https://forums.phpfreaks.com/topic/70194-string-replace-of-variable-in-database/#findComment-352659 Share on other sites More sharing options...
teng84 Posted September 21, 2007 Share Posted September 21, 2007 $get = $_GET['state']; $x = substr($get,0,1); $y = substr ($get,strlen($get)); $sql = "SELECT * FROM states WHERE state LIKE '$x% %$y';"; note not tested but thats the idea of doing that Quote Link to comment https://forums.phpfreaks.com/topic/70194-string-replace-of-variable-in-database/#findComment-352661 Share on other sites More sharing options...
teng84 Posted September 21, 2007 Share Posted September 21, 2007 BTW i only get that first and last letter you can try to get the first 3 and last 3 letters for better matching Quote Link to comment https://forums.phpfreaks.com/topic/70194-string-replace-of-variable-in-database/#findComment-352663 Share on other sites More sharing options...
dnk37482 Posted September 22, 2007 Author Share Posted September 22, 2007 thanks...I think this helps.... but how would I get 3 like you said for better matching? Quote Link to comment https://forums.phpfreaks.com/topic/70194-string-replace-of-variable-in-database/#findComment-352734 Share on other sites More sharing options...
dnk37482 Posted September 22, 2007 Author Share Posted September 22, 2007 also this wont work if a value is three words right? like "x xxx xxxx" Quote Link to comment https://forums.phpfreaks.com/topic/70194-string-replace-of-variable-in-database/#findComment-352740 Share on other sites More sharing options...
MmmVomit Posted September 22, 2007 Share Posted September 22, 2007 Is this what you're looking for? SELECT REPLACE(state, ' ', '') AS state FROM states; Quote Link to comment https://forums.phpfreaks.com/topic/70194-string-replace-of-variable-in-database/#findComment-352751 Share on other sites More sharing options...
dnk37482 Posted September 24, 2007 Author Share Posted September 24, 2007 Is this what you're looking for? SELECT REPLACE(state, ' ', '') AS state FROM states; isnt this just the same thing as a string replace though, I cant call this from states.php?id=newyork I would still have to call states.php?id=new york Or am I wrong? Quote Link to comment https://forums.phpfreaks.com/topic/70194-string-replace-of-variable-in-database/#findComment-353753 Share on other sites More sharing options...
teng84 Posted September 24, 2007 Share Posted September 24, 2007 i guess better to do it in normal way like new york or do make an alias like state = NY better , shorter and cleaner than having new%york in your URL Quote Link to comment https://forums.phpfreaks.com/topic/70194-string-replace-of-variable-in-database/#findComment-353758 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.