0

我按照以下步骤在 CentOS 中安装了 memcached

这是 PHP 错误:

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

代码:

/* 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;
}

我这样调用函数:

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

我错过了什么?

4

2 回答 2

2

$memcache对象在此处超出范围

将以下行添加到函数的顶部:

global $memcache;

或者将其作为参数传递:

function cache_query($sql, $nombre, $memcache, $tiempo = -1) {
    ...
}
$result = cache_query('select * from users','all_users', $memcache);
于 2012-05-27T19:13:37.903 回答
0

你需要做:

global $memcache;

在你的功能中。

有关变量范围的信息,请参阅此:

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

于 2012-05-27T19:13:39.377 回答