Jump to content

How can i do this in a better way ?


Nuv

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
https://forums.phpfreaks.com/topic/230678-how-can-i-do-this-in-a-better-way/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.