Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8019413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:22:56+00:00 2026-06-04T21:22:56+00:00

i made this function: /* MEMCACHE */ function cache_query($sql,$nombre,$tiempo = -1){ $cache = new

  • 0

i made this function:
/* MEMCACHE */

function cache_query($sql,$nombre,$tiempo = -1){
    $cache = new Memcache();
    $cache->pconnect('localhost',11211);
    $query_cacheada = $cache->get($nombre);
    if ( $query_cacheada  === false ) {
             /* key not in memcache, perfom query, cache it and return */
         $res = mysql_query($sql);
         $cache->set($nombre,$res, 0, 60*60*24);  
         return $res; /* this looks good */
    }else{
             /* key in memcache, just return cached  */
         return $query_cacheada;  /* this doesnt return right elements */
    }
}

wich i am using so:

class text{

    protected $id;
    protected $key;
    protected $language;
    protected $text;

    function __construct($clave,$lan){
       $consulta = cache_query("SELECT * FROM textos  
                                WHERE clave = '$clave' AND lengua = '$lan'" ,"TRANSLATION_".$clave."_".$lan);


        if(mysql_num_rows($consulta)>0){
            while($item = mysql_fetch_array($consulta)){
                $this->id = $item['id'];
                $this->clave = $item['key'];
                $this->lengua = $item['language'];
                $this->texto = $item['text'];

            }
                return true;
         }
    }
    function get_text(){
          return $this->text;
    }
}
function translation($key,$language){
     $tem = new text($key,$language);
     return $tem->get_text();
}

then:

$translationText = translation('hello','fr');

The problem is that it stores in cache arrays (always zero), var_dump($m->get(k)) returns:

int(0) int(0) int(0) int(0) int(0) int(0) int(0) int(0) int(0) int(0) int(0) …..

And the $sql query is fine because the rows are collected fine and printed fine, the problem is with the stored value..

I have cleared the cache doing (several times, to make sure the values are not from a previous wrong output):

$consulta = $cache->get($nombre);
             /* manually*/
             $consulta = false;
        if ( $consulta === false) {
            $consulta = mysql_query($sql);
            $cache->set($nombre,$consulta, MEMCACHE_COMPRESSED, 60*60*24);
        };

so.. what am I missing?

EDIT

Here is a codepad, the problem is the mysql_query and memecache is not enabled, but in case someone wants to fiddle with it a bit

http://codepad.viper-7.com/PJNepH

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T21:22:57+00:00Added an answer on June 4, 2026 at 9:22 pm

    You can only cache something that can be serialized. This includes everything but the type resource (which is returned from a successful mysql_query). You need to change your logic a bit so that you are caching an array. Change this:

    $res = mysql_query($sql);
    $cache->set($nombre,$res, 0, 60*60*24); 
    

    To:

    $res = mysql_query($sql);
    $rows = array();
    while($row = mysql_fetch_array($res)) $rows[] = $row;
    $cache->set($nombre, $rows, 0, 60*60*24); 
    

    Then change this:

    if(mysql_num_rows($consulta)>0){
        while($item = mysql_fetch_array($consulta)){
            $this->id = $item['id'];
            $this->clave = $item['key'];
            $this->lengua = $item['language'];
            $this->texto = $item['text'];
    
        }
        return true;
    }
    

    To:

    foreach($consulta as $item){
           $this->id = $item['id'];
           $this->clave = $item['key'];
           $this->lengua = $item['language'];
           $this->texto = $item['text'];
    }
    
    // This is your old code written to work with a 2D array instead of a resource,
    // But this keeps overwriting the same variables in a loop,
    // if you selected multiple rows; otherwise you don't even need a loop and can just do:
    
    $this->id = $consulta[0]['id'];
    $this->clave = $consulta[0]['key'];
    $this->lengua = $consulta[0]['language'];
    $this->texto = $consulta[0]['text'];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made this little function: public String getDay() { String day = (String)android.text.format.DateFormat.format(E, new
I made this function, the same result get printed twice. Any idea why? is
i've made this function: function fetchGeo(){ var geoInfo = new Array(); var counter =
I made this function to verify a user's twitter credentials. Its running on two
Hi have made this function which is made to replicate an error that I
I made this little function from code snippets around the net. It does what
I made this simple function to filter the data. I add the symbols that
I made this bookmarklet: javascript:(function(){var s=document.createElement('script');s.setAttribute('src','http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js');document.getElementsByTagName('body')[0].appendChild(s);$('#hldIntMain').hide();$('#fadeBackground').hide();return false;})() Formatted code: // Add in jQuery var
I've made this snippet that clicks a link after 10th second: function timeout() {
I made a fancy shape with OpenGL and I draw that shape this function:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.