How get all keys in redis using php redis?
Asked Answered
U

3

10

I am using https://github.com/nicolasff/phpredis extension to access redis. I want to get all keys in redis from PHP code. I tried following code:

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$allKeys = $redis->keys('*');
print_r($allKeys); // nothing here

But following command in shell giving results:

127.0.0.1:6379> KEYS *
"kq92p7b5tf63tmk12v54373e03 hs7ep4lc2m6ci5kk5dosgpelg4 
pt7lfejenqbmmovjpmp9aojuf0 2p05gf20or6r5ee5i7sts90kn1 
cb1d6g3d3bvqetjfmkmaurmpp3"

I am able to set key and data in following manner from PHP script:

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->set(session_id(), json_encode(array('uname'=>'messi fan')));

How to get KEYS * from redis using phpredis ?

Undercut answered 12/2, 2014 at 5:39 Comment(1)
Have you found a way to get all keys using phpredis?Scheld
T
9

There is nothing wrong with your code. You are doing it correctly: $redis->keys('*') retrieves all the keys.

The result:

"kq92p7b5tf63tmk12v54373e03 hs7ep4lc2m6ci5kk5dosgpelg4 
pt7lfejenqbmmovjpmp9aojuf0 2p05gf20or6r5ee5i7sts90kn1 
cb1d6g3d3bvqetjfmkmaurmpp3"

is in fact the key that you set when you did:

 $redis->set(session_id(), json_encode(array('uname'=>'messi fan')));

So session_id() returned the value:

kq92p7b5tf63tmk12v54373e03 hs7ep4lc2m6ci5kk5dosgpelg4 
pt7lfejenqbmmovjpmp9aojuf0 2p05gf20or6r5ee5i7sts90kn1 
cb1d6g3d3bvqetjfmkmaurmpp3

therefore this became the name of the key that you set.

Thoria answered 12/2, 2014 at 9:28 Comment(0)
C
4
$redis = new Redis();
$redis->connect('xxxxxx', 6379); // use your Host from Redis desktop Manager - connection
$redis->auth('xxxxxx'); // use your Auth from Redis desktop Manager - connection
$allKeys = $redis->keys('*');
print_r($allKeys); // nothing here
Cleruchy answered 26/1, 2017 at 11:15 Comment(1)
The post included your proposed solutions and it didn't work.Tala
S
-9

Try this

 $redis->get('key');
Schwann answered 12/2, 2014 at 5:53 Comment(1)
$redis->keys('*'); Will get you all the keysVodka

© 2022 - 2024 — McMap. All rights reserved.