Solarpitch Posted October 24, 2009 Share Posted October 24, 2009 Hey Guys, I have a table with a username column. When people registered to the site I allowed them to enter a username using spaces, but now I want to make their username into a subdomain so they can go myusername.example.com Is there any way I can run a command so remove any Spaces from the username? Eg: Currently: Solapitch Boy 41 -> solarpitchboy41 Usr Delta - > usrdelta .. and so on Link to comment https://forums.phpfreaks.com/topic/178841-solved-removing-spaces-in-a-column/ Share on other sites More sharing options...
cags Posted October 24, 2009 Share Posted October 24, 2009 One method would be this... $result = mysql_query("SELECT * FROM table"); $subdomains = array(); while($row = mysql_fetch_assoc($result)) { $subdomains[] = array('id'=>$row['id'], 'subdomain'=>str_replace(" ", "", $row['username']); } foreach($subdomains as $k=>$v) { mysql_query("UPDATE table SET subdomain='". $v['subdomain'] . "' WHERE id='" . $v['id'] . "'"); } ...but it does seem a bit on the long winded side. You could probably combine the loops if you wanted. MySQL may have a method for removing spaces to allow it in a single query, but if it doesn't I don't know it. Having said that, you will only need to run this function once, so I don't suppose it matters a great deal. Link to comment https://forums.phpfreaks.com/topic/178841-solved-removing-spaces-in-a-column/#findComment-943483 Share on other sites More sharing options...
PFMaBiSmAd Posted October 24, 2009 Share Posted October 24, 2009 UPDATE your_table SET your_column = REPLACE(your_column,' ','') Link to comment https://forums.phpfreaks.com/topic/178841-solved-removing-spaces-in-a-column/#findComment-943491 Share on other sites More sharing options...
cags Posted October 24, 2009 Share Posted October 24, 2009 Ahh, thought it would be possbile. Link to comment https://forums.phpfreaks.com/topic/178841-solved-removing-spaces-in-a-column/#findComment-943503 Share on other sites More sharing options...
Solarpitch Posted October 24, 2009 Author Share Posted October 24, 2009 Thanks Guys, PFMaBiSmAd, that command was perfect. Link to comment https://forums.phpfreaks.com/topic/178841-solved-removing-spaces-in-a-column/#findComment-943504 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.