Jump to content

Recommended Posts

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.