beginner's MySQL/memcache/PHP question

Aaron Stone aaron at serendipity.cx
Wed Nov 7 23:48:06 UTC 2007


Josh,

The value in $result is a handle to result set of the query. It has no
usefulness other than to make mysqli calls to fetch the data that's in the
result set.

What you want to do is write a loop like this:

    $result = $db->query($sql);

    while ($data = $db->fetch_array($result) {
        $key = $data["your_id_column"];
        $val = $data["your_data_column"];
        $memcache->set($key, $val, TRUE, 86400);
    }

Aaron


On Wed, Nov 7, 2007, Josh Meyer <jmeyer at msg.ucsf.edu> said:

> 
> I use mysqli in PHP 5 right now and want to start incorporating the use 
> of memcache.  I'm able to store something in memcache, but I'm not quite 
> able to get things out. 
> 
> I put data in with this:
>     $db = new mysqli('host','user','password','database');
>     $sql = ... <a SELECT statement>
>     $result = $db->query($sql);
>     $memcache->set($memcache_key,$result,TRUE,86400) or die ("Failed to 
> save data at the server");
> 
> The output of this:
>     $cached_result = $memcache->get($memcache_key);
>     var_dump($cached_result);
> 
> is this:
>     object(mysqli_result)#2 (0) { }
> 
> So, shouldn't I be able to treat $cached_result as a mysqli result set?  
> Shouldn't something like the following work (it doesn't)?
>     $num_results = $cached_result->num_rows;
> 
> Should I massage $result into something else before sticking it into 
> $memcache->set()?
> 
> Thanks in advance,
> Josh
> 
> 

-- 





More information about the memcached mailing list