如何在整个mysql数据库上使用ucwords()php函数?

时间:2014-02-26 01:31:40

标签: php mysql function

我是MySQL和PHP的完全初学者,我正在尝试使用以下代码清理乱码表,这似乎没有任何影响:

<html>
<head>
<title> Upper Case Words </title>   
</head>
<body>
    <?php
        $con = mysql_connect("localhost", "root", "" );
        if (!$con) 
            {
                die("Cannot Connect: " . mysql_error());
            }

        $sql = ("UPDATE customerstable 
                SET firstname = ucwords(strtolower(firstname));

                UPDATE customerstable 
                SET lastname = ucwords(strtolower(lastname));

                UPDATE customerstable 
                SET gender = ucwords(strtolower(gender));

                UPDATE customerstable 
                SET reg_type = ucwords(strtolower(reg_type));

                UPDATE customerstable 
                SET job_title = ucwords(strtolower(job_title));

                UPDATE customerstable 
                SET address_type = ucwords(strtolower(address_type);

                UPDATE customerstable 
                SET address1 = ucwords(strtolower(address1));

                UPDATE customerstable 
                SET address2 = ucwords(strtolower(address2));

                UPDATE customerstable 
                SET town = ucwords(strtolower(town));

                UPDATE customerstable 
                SET county = ucwords(strtolower(county))");

        $retval = mysql_query($sql, $con);

        if (!$retval)
            {
                die("Could not update:" . mysql_error());
            }
        echo "Updated Successfully\n";

        mysql_close($con);
    ?>
</body>
</html>

我花了几个小时试图找出我在这里做错了什么,我很欣赏我可能会犯下根本性的错误......基本上我需要在表格中正确处理所有单词。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

这对您有用...请注意,您需要更改表ID的ID。您应该在管理中提供一个链接,以便在需要时运行它,或者甚至更好地将cron设置为每天运行或产生影响。

更好的编程方法是,当信息传递给注册插入数据库并更新mysql查询时,将ucwords()放在每一列上。

<html>
<head>
<title> Upper Case Words </title>   
</head>
<body>
    <?php
        $con = mysql_connect("localhost", "root", "" );
        if (!$con) 
            {
                die("Cannot Connect: " . mysql_error());
            }

            $result = mysql_query("SELECT * FROM customerstable WHERE id >=1");

            while($row = mysql_fetch_array($result))
              {          
                $id = $row['id']; 
                $firstname = $row['firstname'];               
                $lastname = ucwords($row['lastname']);                
                $gender = ucwords($row['gender']);                
                $reg_type = ucwords($row['reg_type']);                
                $job_title = ucwords($row['job_title']);                
                $address_type = ucwords($row['address_type']);                
                $address1 = ucwords($row['address1']);                
                $address2 = ucwords($row['address2']);                
                $town = ucwords($row['town']);                
                $county = ucwords($row['county']);

                $sql = 'UPDATE customerstable SET 

                lastname = '.$lastname.',                
                gender = '.$gender.',                
                reg_type = '.$reg_type.',                
                job_title = '.$job_title.',                
                address_type = '.$address_type.',                
                address1 = '.$address1.',                
                address2 = '.$address2.',                
                town = '.$town.',                
                county = '.$county.'
                WHERE id='.$id;
                     $retval = mysql_query($sql, $con);
                   if (!$retval)
                   {
                   die("Could not update:" . mysql_error());
                   }
                   $update_msg .=  $id."] Updated Successfully\n";


        }           

     if($update_msg){echo $update_msg;}

mysql_close($con);

    ?>
</body>
</html>