Thursday, January 15, 2015

#2006 - MySQL server has gone away

Error : #2006 - MySQL server has gone away
 
Soln :-
 
This error is occur due to due to expire of wait_timeout .
 
Just go to mysql server check its wait_timeout :
 
mysql> SHOW  VARIABLES  LIKE  'wait_timeout'
 
mysql> set global wait_timeout = 600 # 10 minute or maximum wait time out you need  

Monday, November 10, 2014

django.core.exceptions:ImproperlyConfigured: Error importing module django.contrib.auth.middleware: "cannot import name utils"

When I deploy my code My site stop working properly from One of my server below error occurs . I try to redeploy my code restart Apache 2-3 times .But still below error show in my error log :-

django.core.exceptions:ImproperlyConfigured

/django.core.handlers.wsgi:WSGIHandler.__call__

Traceback (most recent call last):
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/newrelic-2.14.0.11/newrelic/api/web_transaction.py", line 853, in __call__ result = application(environ, _start_response)
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/newrelic-2.14.0.11/newrelic/api/function_trace.py", line 90, in literal_wrapper return wrapped(*args, **kwargs)
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 187, in __call__ self.load_middleware()
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/newrelic-2.14.0.11/newrelic/common/object_wrapper.py", line 277, in _wrapper result = wrapped(*args, **kwargs)
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/core/handlers/base.py", line 47, in load_middleware mw_class = import_by_path(middleware_path)
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/utils/module_loading.py", line 26, in import_by_path sys.exc_info()[2])
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/utils/module_loading.py", line 21, in import_by_path module = import_module(module_path)
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module __import__(name)
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/contrib/auth/middleware.py", line 3, in from django.contrib.auth.backends import RemoteUserBackend
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/contrib/auth/backends.py", line 3, in from django.contrib.auth.models import Permission
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/contrib/auth/models.py", line 48, in class Permission(models.Model):
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/db/models/base.py", line 96, in __new__ new_class.add_to_class('_meta', Options(meta, **kwargs))
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/db/models/base.py", line 264, in add_to_class value.contribute_to_class(cls, name)
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/db/models/options.py", line 124, in contribute_to_class self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/db/__init__.py", line 34, in __getattr__ return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/db/utils.py", line 198, in __getitem__ backend = load_backend(db['ENGINE'])
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/db/utils.py", line 113, in load_backend return import_module('%s.base' % backend_name)
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module __import__(name)
File "/opt/.virtualenv/mysite/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 39, in from django.db import utils
ImproperlyConfigured: Error importing module django.contrib.auth.middleware: "cannot import name utils"


To solve this problem I reinstall django 1.6 & then resart apache.

pip uninstall django
pip install django==1.6.7
service apache2 restart

 
 

Wednesday, December 5, 2012

Saving cookie as an array


Every Browser have limit of setting cookie by domain And it is best practice  if save your cookie in form of array .Browser support saving cookie in form :-
1) name,value
2) Json
3) Serialized

So I have Create a class Cookie Manager in Php for saving , getting & checking cookie in form of array using serialized - unserialized.


/**
 * Description of CookieManager
 *
 * @author saurabh goyal
 */
class CookieManager {
    protected  $path;
    protected  $domain;
    protected  $expire;
    protected  $secure;
   
    /**
     * __construct
     * @param mixed $path,$domain,$expire
     * @param boolean $secure,$httpOnly
     * @param mixed $objResumeUpdateManager
     * @return void
     */
  
    public function __construct($path='/',$domain='.localhost',$expire='',$secure=false,$httpOnly=false) {       
        $this->path = $path;
        $this->domain = $domain;
        $this->expire = $expire;
        $this->secure = $secure;
        $this->httpOnly = $httpOnly;
    }
   
    public function setCookie($name,$value){
        setcookie($name, $value, $this->expire, $this->path, $this->domain, $this->secure,$this->httpOnly);       
    }
      /*
     * Set setCookie
     * @param string $name
     * @param string $value
     * return boolean
     */
    public function setSerializeCookie($name,$value) {
        try {           
            $this->setCookie($name,serialize($value));
            return true;
        } catch (Exception $e) {
            return false;
        }
    }

    public function getUnSerializeCookie($name) {
        return (isset($_COOKIE[$name]) && $_COOKIE[$name]!='')?unserialize($_COOKIE[$name]):false;
    }

     /*
     * Set setCookieArray
     * @param string $name
     * @param array $arrayValue
     * return boolean
     */

    public function setCookieArray($name,$arrayValue) {
        $cookieArray = $this->getUnSerializeCookie($name);
        if($cookieArray) {
             $arrayValue = $arrayValue+$cookieArray;
        }
        $this->setSerializeCookie($name,$arrayValue);
        return true;
    }
  
    /*
     * Set unsetCookieArray
     * @param string $name
     * @param string $value key of array
     * return boolean
     */
    public function unsetCookieArrayByKey($name,$key) {
         $cookieArray = $this->getUnSerializeCookie($name);
         if($cookieArray && (array_key_exists($key,$cookieArray) !== false)) {
             unset($cookieArray[$key]);            
             $this->setSerializeCookie($name,$cookieArray);
         }
         return true;
    }

    public function getCookieValueByKey($name,$key) {
        $cookieArray = $this->getUnSerializeCookie($name);
        if($cookieArray) {
           return isset($cookieArray[$key])?$cookieArray[$key]:false;
        }
        return false;
    } 
      
}

?>





And Javascript code for same :-



Tuesday, August 23, 2011

Funtion of Sorting an Array using Php

$arr = array(1,7,6,8,4,5,2);

function sg_array_sort($arr) {
    for($i=1;$i      for($j=$i-1;$j>=0;$j--) {   
         if($arr[$i] < $arr[$j]) {
            $temp = $arr[$i];
            $arr[$i]= $arr[$j];
            $arr[$j] = $temp;
            $i = $j;
         } 
      }
    }
}


$sort_arr =  sg_array_sort($arr);


Many-to-many MySql relation: how to retrieve all related entries in one query?

Suppose there are 3 Table 1) Products , 2)Category & 3)Pro_Cat :- Many to Many RelationShip B/w Products & Category Table :-

1) Products Table :-                                  


Product_Id
Product_Name
1
Samsung
2
Relience
3
Micromax

2) Category Table :-


Category_Id
Category_Name
1
Mobile
2
Retails
3
Usb

3) Product_Category Many into Many Relationship Table :-


Product_Id
Category_Id
1
1
2
1
2
2
3
1
3
3


Result Query :-

SELECT
  product.product_name,
  GROUP_CONCAT(category.category_name) AS category_name
FROM product,category,pro_cat
Where product.product_id = pro_cat.product_id
and category.category_id = pro_cat.category_id
GROUP BY product.product_id;

Result :-


Product_Name
Category_Names
Samsung
Mobile
Relience
Mobile,Retails
Micromax
Mobile ,Usb