Redis HGETALL in PHP (Detailed Guide w/ Code Examples)

Use Case(s)

The HGETALL command in Redis is used when there is a need to retrieve all the fields and values of a hash stored at a key. In PHP, it's commonly used when you want to fetch the complete set of data in a hash structure, like user profiles, product details, or any other sets of key-value pairs.

Code Examples

Let's say that we have a hash in Redis representing a user profile where the key is user:1, and it has fields like name and email.

Here's how you can use the HGETALL command in PHP using the phpredis extension:

$redis = new Redis(); $redis->connect('', 6379); $user = $redis->hGetAll('user:1'); print_r($user);

This will output an associative array with the field names as keys and field values as values. For example:

    [name] => John Doe
    [email] =>

Best Practices

  • Always check if the key exists before calling hGetAll() to prevent unnecessary errors.
  • If your application requires frequent reads of large hashes, consider implementing pagination using the HSCAN command instead of HGETALL to avoid potential performance issues.

Common Mistakes

  • Confusing HGETALL with GET. Remember, HGETALL is used for hashes in Redis, not simple key-value pairs which use GET.
  • Using HGETALL on large hashes can lead to high memory usage and slower response times. Use this command judiciously and consider alternatives like HSCAN for large data sets.


Q: Can I use HGETALL to get all keys in Redis? A: No, HGETALL is used to retrieve all the fields and values of a hash at a specific key. To get all keys in Redis, you would use the KEYS command instead.

Was this content helpful?

Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.