All posts by WhiteWind

About WhiteWind

I'm a Perl-PHP Developer from India. I'm a great fan of Wordpress & Perl.

UPDATE multiple tables in single query?

It’s as follows :-

UPDATE table1 a INNER JOIN table2 b ON a.task_id = b.task_id SET a.column='val' b.column=UNIX_TIMESTAMP(CONVERT_TZ(CURRENT_TIMESTAMP,'+05:30','+00:00')) WHERE a.task_id='1024' AND b.task_id='1024'

INSERT a data from a table to another in a single query…

It’s as follows…

INSERT INTO table2 SELECT * FROM table1;

or to copy a few columns

INSERT INTO table2 (column_name(s)) SELECT column_name(s) FROM table1;

They can also be written as follows

SELECT column_name(s) INTO newtable [IN externaldb] FROM table1;

or to copy a few columns

SELECT column_name(s) INTO newtable [IN externaldb] FROM table1;

How to get array length in php?

It’s as follows :-

<?php
$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
$result = count($a);       //we get the length of array here.
?>

PHP class to generatre UUID

It can be written as follows :-

    class uuidgen
    {
        function uuid($serverID=1)
        {
            $t=explode(" ",microtime());
            return sprintf( '%04x-%08s-%08s-%04s-%04x%04x',
                $serverID,
                $this->clientIPToHex(),
                substr("00000000".dechex($t[1]),-8),   // get 8HEX of unixtime
                substr("0000".dechex(round($t[0]*65536)),-4), // get 4HEX of microtime
                mt_rand(0,0xffff), mt_rand(0,0xffff));
        }
        function uuidDecode($uuid)
        {
            $rez=Array();
            $u=explode("-",$uuid);
            if(is_array($u)&&count($u)==5)
            {
                $rez=Array(
                    'serverID'=>$u[0],
                    'ip'=>$this->clientIPFromHex($u[1]),
                    'unixtime'=>hexdec($u[2]),
                    'micro'=>(hexdec($u[3])/65536)
                );
            }
            return $rez;
        }
        function clientIPToHex($ip="")
        {
            $hex="";
            if($ip=="")
            {
                $ip=getEnv("REMOTE_ADDR");
            }
            $part=explode('.', $ip);
            for ($i=0; $i<=count($part)-1; $i++)
            {
                $hex.=substr("0".dechex($part[$i]),-2);
            }
            return $hex;
        }
        function clientIPFromHex($hex)
        {
            $ip="";
            if(strlen($hex)==8)
            {
                $ip.=hexdec(substr($hex,0,2)).".";
                $ip.=hexdec(substr($hex,2,2)).".";
                $ip.=hexdec(substr($hex,4,2)).".";
                $ip.=hexdec(substr($hex,6,2));
            }
            return $ip;
        }
    }

To get uuid call the class as follows :-


$x=$uuid->uuid();
echo $x

How to replace a content using regular expression search in php?

It’s as follows :-

 <?php
$string = 'April 15, 2003';
$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = '${1}1,$3';
echo preg_replace($pattern, $replacement, $string);
?> 
O/p :-
April1,2003