Alter data in database..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
dashwood75
Forum Newbie
Posts: 13
Joined: Fri Nov 04, 2005 8:12 am
Location: UK

Alter data in database..

Post by dashwood75 »

Basically I need to change the information in a field from the format of lastname, firstname to firstname lastname.

This is in a SQL database, I know how to connect to the database and read the data out using php but not too sure on how to manipulate it then update the record... 8O

Help is appreciated :D
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You need an UPDATE statement.

Code: Select all

$query = "select id, name from tablename";
$result = mysql_query($query);

$tmp = array();
while($row = mysql_fetch_assoc($result))
{
    $parts = explode(', ', $row['name']);
    
    $query2 = "update tablename set name = '{$parts[1]} {$parts[0]}' where id = {$row['id']}";
    mysql_query($query2);
}
I strongly recommend you think about the design of a database better though... those values should in separate columns.
User avatar
dashwood75
Forum Newbie
Posts: 13
Joined: Fri Nov 04, 2005 8:12 am
Location: UK

Post by dashwood75 »

I know that but as it is the database for our helpdesk application I didnt have much choice!

Database written courtesy of Intuit.
Post Reply