2014-02-26 109 views
0

我既MySQL和PHP的一個初學者,我嘗試使用下面的代碼似乎沒有被任何有效果清理凌亂的桌子:如何在整個mysql數據庫上使用ucwords()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> 

我已經花了几几小時試圖找出我在這裏做錯了什麼,我很欣賞我可能犯了基本錯誤......基本上我需要正確處理表中的所有單詞。任何幫助將不勝感激。

+0

你不能在這樣 – kero

+0

伴侶SQL語句中使用PHP函數,你的代碼是開發有危險開放。你應該真的考慮使用MySQLi,或者至少加強一點。 – ggdx

+0

好的,謝謝你們......我猜我應該放棄這條路線,因爲我太離譜了。感謝指針@kingkero ... theres在MySQL中該線程中的有用鏈接。 – columb47

回答

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> 
相關問題