gpt4 book ai didi

php - 为什么 memcache->get() 只从数据库中检索第一行?

转载 作者:行者123 更新时间:2023-11-29 05:36:30 25 4
gpt4 key购买 nike

第一次接触内存缓存;这是我的代码:

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

// set the key then check the cache
$key = md5(' SELECT * FROM `users` ');
$get_result = $memcache->get($key);

if($get_result) {
echo "Data Pulled From Cache";
var_dump($get_result);
}
else {
$query = ' SELECT * FROM `users` ';
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$memcache->set($key, $row, TRUE, 20); // stored for 20 seconds

echo "Data Pulled from the Database";
var_dump($row);
}

我的用户表中有 50,000 个虚拟用户,为什么 var dump 只显示第一个用户?如您所见,查询中没有限制子句。

最佳答案

您只从 $result 资源中获取一行。如果你想要一个完整的行集,你需要在 while 循环中获取每一行并将它们附加到一个数组中:

  $rowset = array();
while($row = mysql_fetch_assoc($result)) {
// Fetch all rows in loop, appending each onto $rowset
$rowset[] = $row;
}
// Store $rowset into memcache
$memcache->set($key, $rowset, TRUE, 20); // stored for 20 seconds

关于php - 为什么 memcache->get() 只从数据库中检索第一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10098817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com