Blog Details

Home Blogs

blog-img01

PHP

PHP

Memcached Working with PHP
Description
<?php

/*=======connect Memcache===============*/ 

        $m = new Memcache;     

  $m->connect('localhost', 11211) or die ("Could not connect"); 

  $expires = 60*60*24*30; // seconds, minutes, hours, days 

  $data[] = (object) array('UnitId'=>'1', 'UnitName'=>'pic'); 

  $data[] = (object) array('UnitId'=>'2', 'UnitName'=>'bundle'); 

  $arr['rows']=$data; 

  $str=json_encode($arr);  

  /*=======Memcache Object Ready=========*/ 

  //echo $str; 

  $obj = new stdClass;   

  $obj->str = $str; 

  /*=======Set Memcache Obj==============*/ 

  $m->set('key', $obj, false, $expires );     

  /*=======Delete Memcache Obj===========*/ 

  //$m->delete('key');             

  /*=======Fetch Memcache Obj============*/ 

  $get_result = $m->get('key');         

  /*=======Print Memcache JSON obj=======*/ 

  echo $get_result->str;     

    

?>

List of all Column in a MySql Table
Description
<?php

  $host_name='localhost'; 

  $db_name="dgfplmis_db"; 

  $UserID="root"; 

  $Password=""; 

  $connection=mysql_connect($host_name,$UserID,$Password) or die("Could not connect to server"); 

  $db=mysql_select_db($db_name,$connection) or die("Could not select database"); 

  $sql="SHOW COLUMNS FROM `2012_f7_tab` "; 

  $result=mysql_query($sql,$connection); 

  while($row=mysql_fetch_assoc($result)){     

    $arr[]=$row['Field']; 

    //print_r($row); 

  } 

  echo json_encode($arr); 

    

?>

List of All Files in a Directory
Description
<?php

//path to directory to scan 

$directory = "images/project1/620x378/"; 

//get all image files with a .jpg extension. 

$images = glob($directory . "*.jpg"); 

//print each file name 

foreach($images as $image) 

{ 

echo $image; 

} 

    

?>

XML Generation
Description
<?php

header(“Content-type: text/xml”);

$str= “<?xml version=’1.0′ encoding=’ISO-8859-1′?>”;

$str.= “<note>”;

$str.= “<from>Nayeem</from>”;

$str.= “<to>Fariha</to>”;

$str.= “<message>Remember me this weekend</message>”;

$str.= “</note>”;

echo $str;

?>

PHP E-mail Sending
Description
<?php

$to .= ‘
[email protected]
This email address is being protected from spambots. You need JavaScript enabled to view it.
‘;

// subject

$subject = ‘Birthday Reminders for August’;

// message

$message = ‘

<html> <head> <title>Birthday Reminders for August </head>

<body> <p>Here are the birthdays upcoming in August!</p> <table> <tr> <th>Person</th><th>Day</th><th>Month</th><th>Year</th> </tr> <tr> <td>Joe</td><td>3rd</td><td>August</td><td>1970</td> </tr> </table>

</body> </html> ‘;

// To send HTML mail, the Content-type header must be set

$headers = ‘MIME-Version: 1.0’ . “\r\n”;

$headers .= ‘Content-type: text/html; charset=iso-8859-1’ . “\r\n”;

// Additional headers

$headers .= ‘To: Mary <
[email protected]
This email address is being protected from spambots. You need JavaScript enabled to view it.
>, Kelly <
[email protected]
This email address is being protected from spambots. You need JavaScript enabled to view it.
>’ . “\r\n”;

$headers .= ‘From: Birthday Reminder <
[email protected]
This email address is being protected from spambots. You need JavaScript enabled to view it.
>’ . “\r\n”;

$headers .= ‘Cc:
[email protected]
This email address is being protected from spambots. You need JavaScript enabled to view it.
‘ . “\r\n”;

$headers .= ‘Bcc:
[email protected]
This email address is being protected from spambots. You need JavaScript enabled to view it.
‘ . “\r\n”;

// Mail it

mail($to, $subject, $message, $headers);

?>
\n This email address is being protected from spambots. You need JavaScript enabled to view it.
>
\n This email address is being protected from spambots. You need JavaScript enabled to view it.
>
\n This email address is being protected from spambots. You need JavaScript enabled to view it.
>

MySql Connection
Description
<?php

    require(“define.inc”);

    $conn=mysql_connect(HOSTNAME, DBUSER, DBPWD) or die(‘Could not connect: ‘ . mysql_error());

    mysql_select_db(DBNAME, $conn) or die(‘Could not connect: ‘ . mysql_error());

    $task=”;

    if(isset($_POST[‘action’])){

        $task=$_POST[‘action’];

    }else if(isset($_GET[‘action’])){

        $task=$_GET[‘action’];

    }

   switch($task){

       case ‘getElectionDays’:

          getElectionDays($conn);

       break;

    default :

       echo “{failure:true}”;

    break;

   }

?>

<?php

// “define.inc”

   define(‘HOSTNAME’,”localhost”);

   define(‘DBNAME’,”dccelection”);

   define(‘DBUSER’,”dccelection”);

   define(‘DBPWD’, “mlcgjnwc9a”);

?>

PHP Sub String
Description
<?php

$rest = substr(“abcdef”, 0, -1); // returns “abcde”

$rest = substr(“abcdef”, 2, -1); // returns “cde”

$rest = substr(“abcdef”, 4, -4); // returns false

$rest = substr(“abcdef”, -3, -1); // returns “de”

$rest = substr(“abcdef”, -2); // returns “ef”

$rest = substr(“abcdef”, -3, 1); // returns “d”

?>
PHP Date Formate
SL No Description
1 if(function_exists(‘date_default_timezone_set’)) { date_default_timezone_set(‘Asia/Almaty’); } else{ putenv(“TZ=Asia/Almaty”); }
2 $transactionDateLocal=date(“Y-m-d H:i:s”,time());
3 $transactionDateLocal=date(“Y-m-d g:i:s a”,time());
4 <?php

$date = “2012-05-19 13:00”;

echo $stamp = strtotime($date) . “<br />”; // outputs 1337432400

echo date(“Y-m-d g:i:s a”,strtotime($date)); // outputs 2012-05-19 1:00:00 pm

?>

MySql Query
SL No Description
1 INSERT INTO table_name VALUES (value1, value2, value3);
2 UPDATE table_name SET column1=value, column2=value2 WHERE some_column=some_value
3 DELETE FROM table_name WHERE some_column = some_value
SQL FOREIGN KEY
SL No Description
1
ALTER TABLE t_orderitems 

ADD CONSTRAINT fk_UserId 

FOREIGN KEY (UserId) 

REFERENCES icat_users(id)
Primary KEY with two fields
SL No Description
1
ALTER TABLE sdp_product_mapADD CONSTRAINT ProductMap PRIMARY KEY (`SDPMapId`,`UpazilaCode`)
Remove a File/Directory
SL No Description
1
$filePath2=SITEDOCUMENT.'scode/assets/'.$LanguageId.'/svg/word_'.$GlyphId.'.svg'; 
    if (file_exists($filePath2)){       
      unlink($filePath2);       
    }
2
rrmdir(SITEDOCUMENT.'scode/assets/'.$LanguageId); 
    function rrmdir($dir) {  
  foreach(glob($dir . '/*') as $file) {  
    if(is_dir($file)) rrmdir($file); else unlink($file);  
  } rmdir($dir);  
}
© 2012-13 Md. Khurshed Alam Nayeem

Join the Conversation

4 Comments

  1. I’m not sure where you are getting your info, but great topic. I needs to spend some time learning more or understanding more.
    Thanks for wonderful information I was looking for this info for my mission.

  2. I’m not sure where you’re getting your info, but great topic. I needs to spend some time learning much more or understanding more.
    Thanks for excellent information I was looking for this information for my mission.

  3. I’m not sure where you’re getting your information, but great topic. I needs to spend some time learning much more or understanding more.
    Thanks for fantastic info I was looking for this information for my mission.

Leave a comment

Your email address will not be published. Required fields are marked *