Page 1 of 1

Alter data in database..

Posted: Tue Mar 14, 2006 7:11 am
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

Posted: Tue Mar 14, 2006 7:26 am
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.

Posted: Tue Mar 14, 2006 8:23 am
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.