Php – Connecting to memcached “which port should use?” (php)

memcachedPHPport

I've installed memcached perfectly and I have created a new instance but i don't understand if I'm connecting to the right port… I always get a false return from get(key)

Here is my code:

    $sql = "SELECT * FROM users";
    $key = md5('q'.$sql); //create an index key for memcache
    $result = $memcache->get($key);//lookup value in memcache
    //check if we got something back
    if($result == null) {
        echo "nothing back";
        $r = mysql_query($sql) or die(mysql_error()." : $sql");//fetch from database
        if(mysql_num_rows($r)> 0) {
        echo "returned";
            $people = array();
            while ($person = mysql_fetch_assoc($r)) {
                $people[] = $person;
            }
            $memcache->set($key,$people,0,3600);//store in memcache
        }
    }
    print_r($result);

thanks in advance

Best Answer

Here is the connect statement:

$memcache = new Memcache;
$memcache->connect('127.0.0.1',11211) or die('Could not connect');

You are missing a memcached connect statement.

Memcached's default port is widely considered to be 11211.

Use something like http://livebookmark.net/memcachephp/memcachephp.zip to test your memcached/php installation.

Related Topic