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 に答える