Memcache: Call to a member function get() on a non-object

柔情痞子 提交于 2019-12-11 17:59:25

问题


I installed memcached in a CentOS following this steps

This is the PHP error:

Fatal error: Call to a member function get() on a non-object in /home/piscolab/public_html/keepyourlinks.com/includes/funciones.php on line 22

Code:

/* MEMCACHE */
$memcache = new Memcache();
$memcache->pconnect('localhost',11211);

/* checks in memcache, if not: query and save to memcache */
function cache_query($sql,$nombre,$tiempo = -1){
    if($tiempo == -1) $tiempo = time() + 24*60*60*365;
    $consulta = $memcache->get($nombre);  /* THIS is the line */
    if ( $consulta === false) {
        $consulta = mysql_query($sql);
        $memcache->set($nombre,$consulta,0,$tiempo);
        $_SESSION['tQ']++;
    }else $_SESSION['tQC']++;
    return $consulta;
}

I call the function so:

$result = cache_query('select * from users','all_users');

What am I missing?


回答1:


The $memcache object is out of scope here.

Add the following line to the top of your function:

global $memcache;

Or pass it as a parameter:

function cache_query($sql, $nombre, $memcache, $tiempo = -1) {
    ...
}
$result = cache_query('select * from users','all_users', $memcache);



回答2:


You need to do:

global $memcache;

In your function.

See this for info on variable scope:

http://php.net/manual/en/language.variables.scope.php



来源:https://stackoverflow.com/questions/10776545/memcache-call-to-a-member-function-get-on-a-non-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!