Search code examples
sql-updatecapitalize

ucwords() is not capitalizing the field text with UPDATE query


i have a name filed which has all the words in lower case. I want to update field as first letter as capital of all words of complete table at once.

im trying the below code which is updating/replacing the field for all rows:

$queryP = "madinah hi madinah";
$queryD = ucwords($queryP);
$pupdt  =   mysql_query("
update media_detail
set test = '$queryD'
");

and the below is not working:

$queryP     = $row['unique_name'];
$queryD = ucwords($queryP);
$pupdt  =   mysql_query("
update media_detail
set test = '$queryD'
");

please help im not getting it to be working.


Solution

  • Your code is correct except that you should do updates in the loop. I also added the data retrieval step (that you probably omitted for brevity) which precedes the loop. Here is the complete code:

    $sql = "SELECT id, unique_name FROM media_detail";
    $result = $conn->query($sql);
    while($row = $result->fetch_assoc()) {
        $id = $row['id'];
        $queryP = ucwords($row['unique_name']);
        $pupdt  =   mysql_query("
            UPDATE media_detail
            SET test = $queryP
            WHERE id = $id
        ");
        $conn->query($pupdt);
    }