Jump to content

How can i do this in a better way ?


Nuv
 Share

Recommended Posts

Hi,

 

I made a code to add 0 infront of any zipcode with 4 digits. I think this can be done in a better way.Maybe using only sql by LEN('string') or DATALENGTH('string'). Can someone please show me how ? Is there any better way ?

 

<?php
  $mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "flourists";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password)
or die("Opps some thing went wrong");
mysql_select_db($mysql_database, $bd) 
or die("Opps some thing went wrong");
$result = mysql_query("SELECT * FROM bakers");
while($row = mysql_fetch_array($result))
{       
    if((strlen($row['pincode'])) == 4 )
    {    
          
          echo  $row['pincode'] ;
          echo "<br />";
          $addzero = "0";
          $newpincode =  $addzero.$row['pincode'];
          echo $newpincode;
          echo "<br />"; 
          echo "<br />"; 
         // will add UPDATE sql query to update $newpincode
    }   
}
?>

 

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.