Search code examples
phpmysqlstringcapitalize

Capitalize the first word outputted from MySQL only


I am looking to capitalize every letter of the first word that is presented by MySQL and PHP and keep the rest of the words lowercase. How could that be accomplished?

It is stored in the database with only the first letter capitalized, so the real trick is to keep the rest of the words lowercase.

Here is the code pulling the words from the database as of today:

<?php

$query0  = "SELECT LCASE(ord) FROM `keywords` ORDER BY RAND()";
$result0 = mysql_query($query0);

while($row0 = mysql_fetch_array($result0, MYSQL_ASSOC))
{
$keyword = $row0['LCASE(ord)'];

echo "$keyword, ";

}
?>

Solution

  • The solution ended up being like this:

    $query0  = "SELECT LCASE(ord) FROM `keywords` ORDER BY RAND()";
    $result0 = mysql_query($query0);
    
    while($row0 = mysql_fetch_array($result0, MYSQL_ASSOC))
    {
    $keywords[] = $row0['LCASE(ord)'];
    }
    echo ucfirst(implode(', ', $keywords));
    echo ".";
    ?>
    

    This presents a line of words that looks like this:

    Deltagende, sosial, pragmatisk, kunnskapsrik, løsningsorientert, samarbeidende, engasjert.

    Instead of:

    deltagende, sosial, pragmatisk, kunnskapsrik, løsningsorientert, samarbeidende, engasjert,

    This solution was the result of a modification of the solution to a different question regarding how to remove the last comma that I posted here Remove last comma or prevent it from being printed at all MySQL/PHP