1

基本上是我第一次使用 memcached ...我正在尝试运行一个简单的查询,但由于某种原因无法正常工作...有关 memcached 的教程不是很好,我不知道为什么我的查询不起作用

    <?php

    //connect to memcached server and MySQL
$db = new PDO('mysql:host=localhost;dbname=test', 'admin', '');
$memcache = new Memcache;
$memcache->connect('127.0.0.1', 11211) or die ("Could not connect"); 


 // Run the query and get the data from the database then cache it
 $category_querry = $db->query('SELECT * FROM category WHERE category_id=1');
 $category_querry->setFetchMode(PDO::FETCH_ASSOC);

while($row = $category_querry->fetch()) {

 $memcache->set(1, $row['category_name'], 30); // Store the result of the query for 20 seconds
  var_dump($memcache->get(1));
 echo "</br> Data Pulled from the Database </br>";
}


$get_result = $memcache->get(1);
var_dump($get_result);

if ($get_result) 
    {
echo "</br>" . $get_result['category_name'];
echo "</br>" .  "Data Pulled From Cache";
}

代码更新现在不知何故我让它进入了 IF 语句,浏览器中显示的是

string(3) "one" 
Data Pulled from the Database 
string(3) "one" 
o
Data Pulled From Cache

然后这向我展示了所有 var_dumps 工作....它到达 if 语句,但不知何故,$get_result['category_name']它只o在应该输出时输出one

为什么???

4

1 回答 1

0

在设置 memcache 时,您没有定义 $key 变量

$memcache->set($key, $row, TRUE, 60);

您需要先定义 $key 才能在 memcache 中设置它

于 2012-07-27T08:38:37.693 回答