Unstoppable Domains

PHP help

Spaceship Spaceship
Watch

Ray

VIP Member
Impact
167
---------------SOLVED---------------

Hi..

I need some php help. I want to remove some data from a string that comes from a mysql database.

Ex: in the database its "first name" I just want it to say just "first" when its displayed without editing the database.
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
PHP:
$row['user'] = 'First Name';
$data = explode(' ',$row['user']);
echo $data[0]; // echos 'First';
 
1
•••
You can also make mySQL do the work (though if you're selecting from large databases, its probably a very bad idea).

Code:
Select SubString(`field`, 1, Locate(' ', `field`) - 1) from `table`


Bruce
 
1
•••
bearruler said:
You can also make mySQL do the work (though if you're selecting from large databases, its probably a very bad idea).

Code:
Select SubString(`field`, 1, Locate(' ', `field`) - 1) from `table`


Bruce
In PHP =
PHP:
$row['user'] = 'first name';

echo substr($row['user'], 0, strpos($row['user'], ' '));

Just as an alternative :tu:
 
1
•••
is there a way to "trim" the word, because it is on every database entry....
 
0
•••
Do you mean you want to delete the first word of every entry in the database?

If so, you can use this mySQL query. (It will update Every entry in the table for the field you specify, so be careful.
Code:
Update `table` set `field` = SubString(`field`, Locate(' ', `field`))

If that isn't what you needed, please elaborate.


Bruce
 
0
•••
no.. i want to remove the "name"
 
0
•••
Replace `field` in `table` with the first word of `field`:
Code:
Update `table` set `field`=SubString(`field`, 1, Locate(' ', `field`) - 1)

You might want to test this first. Eg, use a "Where" condition, so it only updates one row (Maybe where `field2`='somevalue'). You could also do a quick backup by exporting the table data into a text file, then saving it somewhere.

An even better solution would be a Test table that you try it on first.

That way, if the change isn't as expected, your data isn't corrupted.


Bruce
 
Last edited:
0
•••

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Live Options
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back