tags:

views:

273

answers:

2

Hi, I've taken a bit of a memcache script that i've used previously without issue, but on the new page, I don't get any response.

the memcache is in a function which is included from another page. what I do is put the md5 hash the mysql query and store that as the key for the memcached data. if the key isn't in memcache, then I go, create the data, put it into memcache and return it.

I think the code is fairly simple. Here are the important bits (it's a large page creating the output, so you don't need all that, though the 'return' I think is important as I fear that might be where I'm screwing up.

I call the function with

$outList.= outData($getList);

where $getList is a mysql query

The $outList function is

<?php

@$memcache = new Memcache;
@$memcache->connect("localhost",11211);

function outData($getList)
{
    $memVal = @$memcache->get(MD5($getList));
    if($memVal=='')
    {
        $results=mysql_query($getList)or die(mysql_error());

        // then I do a bunch of stuff with the data
        @$memcache->set(MD5($getList), $memVal, false, 60000);
    } 
    return $memVal;
}

I can display all the stuff to create $memVal, but i suspect the error is in the if line, but the same code is used on another page without issues.

Anything look wrong with this?

+5  A: 

with all those @'s suppressing errors, there's no way to know what is failing.

I ran it - sans the @'s and the answer popped right up though - on the line:

$memVal = @$memcache->get(MD5($getList));
#Notice: Undefined variable: memcache in /home/topbit/736160.php on line 9

Where does it get the variable $memcache ? It's not passed into the function.

Alister Bulman
sorry about that, i was using the @ to test when memcache failed or didn't connect. Of course, I should have known that was supressing the error that I should have seen. My bad :(
pedalpete
+2  A: 

The problem is scope. When in a function, variables in global scope do not automatically get passed on, you will have to add them as parameters to the function.

function outData($memcache, $getList)
esnoeijs