predisHow do I use hmset in PHP with Redis?
HMSET is a Redis command used to set multiple fields and values in a hash stored at a key. It can be used in PHP with the Predis library.
Example code and output:
<?php
require 'predis/autoload.php';
Predis\Autoloader::register();
$client = new Predis\Client();
$client->hmset('hashkey', [
'field1' => 'value1',
'field2' => 'value2',
'field3' => 'value3',
]);
echo $client->hget('hashkey', 'field1'); // Output: value1
The code above uses the Predis library to connect to Redis and then uses the hmset command to set multiple fields and values in a hash stored at a key. The hget command is then used to get the value of one of the fields.
Code explanation
require 'predis/autoload.php';- Loads the Predis library.Predis\Autoloader::register();- Registers the Predis autoloader.$client = new Predis\Client();- Creates a new Predis client.$client->hmset('hashkey', [- Uses the hmset command to set multiple fields and values in a hash stored at a key.'field1' => 'value1',- Sets the field1 field to the value1 value.'field2' => 'value2',- Sets the field2 field to the value2 value.'field3' => 'value3',- Sets the field3 field to the value3 value.]);- Closes the array of fields and values.echo $client->hget('hashkey', 'field1');- Uses the hget command to get the value of the field1 field.
Helpful links
- Predis library: https://github.com/nrk/predis
- Redis HMSET command: https://redis.io/commands/hmset
- Redis HGET command: https://redis.io/commands/hget
More of Predis
- How can I use PHP and Redis to retrieve data from a sorted set using ZRANGEBYSCORE?
- How do I install and configure a PHP Redis DLL on a Windows machine?
- How can I use PHP and Redis to get a reverse range of scores?
- How do I install PHP, Redis, and XAMPP?
- How can I troubleshoot a "PHP Redis went away" error?
- How do I install PHP Redis on Ubuntu 20.04?
- How can I use the zscan command in PHP with Redis?
- How can I install and configure PHP and Redis on a Windows system?
- How can I use PHP and Redis to set a time-to-live (TTL) value?
- How can I use PHP and Redis to retrieve a range of values from a sorted set?
See more codes...