predisHow do I use the PHP Redis rawcommand?
Using the rawcommand
method of the Redis
class in PHP
is a great way to execute arbitrary commands on a Redis
server. Here is an example of how to use it:
<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$reply = $redis->rawcommand('SET', 'mykey', 'myvalue');
echo $reply;
Output example
OK
In the example, we connected to a Redis
server running on 127.0.0.1
on port 6379
, and used the rawcommand
method to execute the SET
command with the arguments mykey
and myvalue
. The output of the command is OK
, which indicates that the command was successful.
Code explanation
$redis = new Redis();
- create a newRedis
object.$redis->connect('127.0.0.1', 6379);
- connect to aRedis
server running on127.0.0.1
on port6379
.$reply = $redis->rawcommand('SET', 'mykey', 'myvalue');
- execute theSET
command with the argumentsmykey
andmyvalue
using therawcommand
method.echo $reply;
- print the output of the command.
For more information about using the rawcommand
method, see the PHP Redis documentation.
More of Predis
- How can I use PHP and Redis to retrieve a range of values from a sorted set?
- How can I use Predis with a cluster in PHP?
- How do I install PHP Redis on Ubuntu 20.04?
- How can I use PHP and Redis to retrieve data from a sorted set using ZRANGEBYSCORE?
- How can I use Redis with the Yii PHP framework?
- How can I use PHP and Redis to get a reverse range of scores?
- How can I troubleshoot a "PHP Redis went away" error?
- How do I install and configure a PHP Redis DLL on a Windows machine?
- How do I use PHP and Redis together to create a transaction?
- How can I install and configure Redis on an Ubuntu server running PHP?
See more codes...