How to use memcached with PHP ?


Hello Guys, Today I would like to share with you most widely used Memcached caching with PHP. My last article was about "How to install memcached in WAMP /Windows." Today I will to show you how to use memcached with PHP. You may be wondering what I am talking about, "What is memcached and how it is works?" Here is the solution.

What is Memcached ?

Memcache is basically high performance distributed memory object caching. Simply to say it is allowing us to store datas locally into the web page itself in a "temporary cache". So each and every time you do not have to run database query and get datas from database. Instead of going to database to get data you can fetch from memcache if exists. This technique allow you to build most optimized application.

There are few functions which we are using mainly in memcache. Let us see what are they,

i. Get() – get the value for a specified key

ii. Set() – sets a given key with a given value

iii. Add() – add values to the cache if it is not exists

iv. Replace() – sets datas into cache if key already exists

v. Flush() – removes all keys and cached data

Who uses Memcache ?

Facebook uses memcache

Now a days, lots of well known websites like Facebook, Twitter, Digg, Youtube are using Memcache. Currently Facebook is the biggest Users of Memcache.

How to use Memcached With PHP?

So hope you all people know how to install memcache in your local server (Server) as I have shown you how to install memcache in WAMP in my previous post. So now let us see how to work with memcache.

Step 1:

First create MemCache.php in your local server. Now paste below code into the file.

class MemCache {

public $expireTime = 60; // Set the expire time
private $isEnabled = false; /* Memcache Isenabled */
private $objCache = null;   

    public function __construct() {

        if (class_exists('Memcache')) {

            $this-> objCache = new Memcache();
            if(!$this->objCache->connect('localhost', 11211)) {
                  $this->objCache->connect('localhost', 11211);
                    $this->isEnabled = true;
  public function getDetails() {
             //Set the key and check the cache
      $cacheKey = md5("SELECT * from user_info where user_id='121'");
      $cacheResult = $this-> objCache ->get($cacheKey);

      if ($cacheResult) {
          echo $cacheResult['username'];
          echo $cacheResult['email_id'];
      } else {
          echo $data[‘username’]." -".$data[‘password’] ."-".$data[‘email_id’]." 
"; } } public function setData() { $query="SELECT * from user_info where user_id='121'"; $result = mysql_query($query); $row = mysql_fetch_array($result); //Use MEMCACHE_COMPRESSED to store the item compressed (uses zlib). $data = $this->objCache->set($cacheKey, $row, 0, $this->expireTime); // Store the result in cache for 60 seconds return $data; } // delete data from cache server function deleteData($cacheKey) { $success_val = $this-> objCache ->delete($cacheKey); return $success_val; } }

Step 2:

Create another file called connection.php and paste below code into it.

$hostname = "localhost";
$user = "root";
$password = " ";
$database = "social_network";

$conn = mysql_connect($hostname, $user, $password) 
or die("Ooppsss!! Something went wrong");
mysql_select_db($database, $conn) or die(: Ooppsss !! Could not able to connect database");

That's all we are done. You can include the connection.php top of the page and before using MemCache.php file

This is a simple example how cache works. Hope this post helps you to optimize your web application. Please do share and post your comments here. And If you would like me to write anything else then please contact me. All the best. Have a good day.

How to Install Memcached in Windows 7 / WAMP ?

           How to install memcached in WAMP/ Windows  ?

                 Today I have an new article for you all here. Last time I did post article about server side javascript Node.js. Today I am going to show you how to install Memcache in WAMP 2.1 / Windows. Now a days, lots of well known websites like Facebook, Twitter, Digg, Youtube are using open source, high performance distributed memory object caching. Which is known as Memcached. Lots of peoples are confortable with windows and they are interested to develop application in windows environment only. So here is an article to show you , “ How to Install Memcached in Windows 7 (WAMP) ? “  
                I am using WAMP server 2.1 and PHP version 5.3 in my system. Installation steps are given below –
   Step 1 :  
                 First of all you need to download I search a lot and at last got the appropriate package, which you can download from here.
   Step 2 :
                  Then you need to unzip the package and you will get memcached.exe. Now what you need to do is, paste it in your directory. I have paste it in my E:\memcache\memcached.exe. and my wamp server also in E drive.
   Step 3 :
                  So now open the directory and right click in the memcached.exe and run as administrator. Since we are using windows 7 so it will show directly in right click itself.
   Step 4:
                 Now it’s time to install the memcached in server. To do that, Just open your command prompt as administrator settings and type the path of your executable file and space and –d install then hit enter.
For your reference ,
  D:\> your path memcached.exe –d start
For further reference look at the screenshot which I added below,

Here you may face some problem. It’s may shows some error like “The program can't install because MSVCR71.dll is missing from your computer .“ Which I faced. Screen shots are given below for your reference.


   Step 5 :
                    To solve this issue you need to download the MSVCR71.dll file. I searched a lot for appropriate version and finally I got it. Here is the link You can download from here. Download the dll and paste it inside your system 32 folder which is in C:\Windows\system32  and again go to the command prompt and installation process and hit enter. Now it won’t show you any error. So that you are sure that your memcached installed successfully.

Step 6 :
            After insatalling go to services inside control panel and search for memcached server and start the service. Else you can go to command prompt and type start memcached server. You will get success messege.
Step 7 :
             Finally you have to change configuration in php.ini file to start working with you memcached with PHP. Then only php will configure with memcached. For this you need php_memcached.dll . You can download this dll from this link
              That’s all you have all steps. Now restart WAMP.  You are ready to work with memcached with php. Which will give you good performance.  Hope you like this tutorial and my posts.  

             Please do leave your valuable comments and let me know if you are facing any problem installing it.

Subscribe blog via RSS so that you will get all my updates when ever any new article will post here and do share.

In my next post i will show you how to use memcached with PHP.


Which Framework to use Zend or Codeigniter ??


Hello! Performance is always matters a lot for modern applications where millions of users visit your website daily. I personally like to use MVC framework for web application development. It helps me to build faster, structured and modular application. I also believe unless building enterprise level development you don't require large scale fatty framework like Zend, Symfony etc. You may find many questions on the web like "Which is the best PHP Framework ? " etc. My personal opinion is nothing called "best", until you know to code well. Even using good framework you may code worst. In this article I will discuss why many people prefer CodeIgniter over Zend Framework.

I trust programming should be always clean, enjoyable, fun using latest tools, updated features etc. We use Framework to handle all basic task on ease. There are various reason why Codeigniter popular, also has disadvantage over other frameworks too. I will discuss below.

CodeIgniter Vs Zend

The main reason why I love CodeIgniter because of is its performance. CodeIgniter three times faster then the Zend framework. Also CodeIgniter is faster than other popular PHP MVC frameworks. Take a look at these Benchmark test done by Rasmus Lerdorf(creator of PHP programming), here , here. And also take a look at another benchmark test. So it’s clear that CI is faster than other Framework because of tiny library. Rasmus also told that he liked CodeIgniter because it is faster, lighter and the least like a framework.

Another main reason behind recommendation, it has very low learning curve. Downloads come with a tremendous user guide, which is a great documentation like PHP Manual to get started with. I have been using CodeIgniter for last 2 years for web projects as a beginner level, from my personal experience, CodeIgniter has very easy, understandable structure compared to CakePHP or Zend Framework. If you’re a beginner and want to kick start your development structured way with PHP frameworks than I recommend you to go with CodeIgniter for understanding how MVC works. And jump to some other framework when you are comfortable with.

CodeIgniter coding conventions are flexible and simple. Ironically, CakePHP, Zend has very strict coding conventions. So you should be very careful about such cases while coding.

Still question in mind "Why not Zend Framework and why CodeIgniter?" Yes! Zend is PHP company and strong supporter. Ok, So question goes here is "What is so good about Zend Framework ?" Eventually! answer is Zend contains huge set of libraries and classes compared to any other framework. But slow performer. Ok! here another great news is that You can use Zend Framework’s classes in the CodeIgniter using Hooks available in CI. So you can even use any libraries from ZEND as a component in CI. Is it not great! yes.


Although there are many features may catch your eye to use CodeIgniter but as web development is grown up faster, modern development requires strong object oriented programming for implementing better, robust, modular application. Of course a OOP MVC Framework for the better development, New Framework For Fun, Enjoyable development. In such cases CodeIgniter still lacking. CodeIgniter is still backdated compare to other MVC frameworks in PHP world. Still legacy code into the core where PHP introduced next generation version as PHP7.

Read: 5 Best things you should know about PHP7

Personally, I have learned many things from CodeIgniter as a beginner and towards building easy to use, hugely powerful, Stunningly Fast and enormously efficient modular HMVC Framework as "Cygnite PHP Framework- The New Cool Kid". Version 2.0 is under active development. You may have a look into it for better experience. Some benchmark of v1.3 here, and here

You can use the below comment form to let us know your thoughts and opinion. If you like this article please do not forget to share with your friends. To receive regular updates from us, you can either follow us on Facebook, Twitter, Google+ or you can subscribe to our monthly email newsletter.

Have a good day!

How to populate datas into a select box using jQuery ajax json, php

If you are wondering how to populate select box using jquery ajax then you may find the below post very simple and useful.

Retrieve values and populate the select box using jquery, ajax and PHP.

    type: "POST",
    url: '',
    data: {'categoryID': $("#category").val(),'isAjax':true},
    success: function(data) {

       var select = $("#select"), options = '';

       for(var i=0;i<data.length; i++)
        options += "<option value='"+data[i].id+"'>"+ data[i].name +"</option>";              


And now in your server side page you may write below to fetch data from database.


if (isset($_POST['categoryID'])) {
    $id = trim($_POST['categoryID']);
    $result = array();
    $id = mysql_real_escape_string($id);
    $res = mysql_query("SELECT * FROM subcategories WHERE category_id = $id");
    while ($row = mysql_fetch_array($res)) {

    $result[] = array(
      'id' => $row['subcatid'],
      'name' => $row['description']

    echo json_encode($result);


Follow Us On Facebook Open Source Web Developers by Appsntech facebook group Twitter Open Source Web Developers by Appsntech twitter group Google+ Open Source Web Developers by Appsntech Google group Linkedin Open Source Web Developers by Appsntech, LinkedIn group
Copyright @2011-2015 All rights reserved. Powered By- Sanjoy Dey Productions