Dragonfly

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('127.0.0.1', 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:

```
Array
(
[name] => John Doe
[email] => john@example.com
)
```

Best Practices

Common Mistakes

FAQs

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?

Similar Code Examples

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Switch & save up to 80% 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost