Redis - PHP连接

Redis - PHP连接 首页 / Redis入门教程 / Redis - PHP连接

PHP程序中开始使用Redis之前,需要确保已在计算机上设置了Redis PHP驱动程序和PHP,您可以检查PHP教程以在您的计算机上安装PHP。

现在,让无涯教程检查如何设置Redis PHP驱动程序。

您需要从github存储库 https://github.com/nicolasff/phpredis  下载phpredis ,下载后,将文件解压缩到phpredis目录,在Ubuntu上,安装以下扩展名。

cd phpredis 
sudo phpize 
sudo ./configure 
sudo make 
sudo make install 

现在,将" modules"文件夹的内容复制并粘贴到PHP扩展目录,并在 php.ini 中添加以下行。

extension=redis.so

现在,您的Redis PHP安装已完成

无涯教程网

连接到Redis服务器

<?php 
   //连接到本地主机上的 Redis 服务器
   $redis = new Redis(); 
   $redis->connect('127.0.0.1', 6379); 
   echo "Connection to server sucessfully"; 
   //检查服务器是否正在运行
   echo "Server is running: ".$redis->ping(); 
?>

执行该程序时,将产生以下输出。

Connection to server sucessfully 
Server is running: PONG 

Redis PHP字符串示例

<?php 
   //连接到本地主机上的 Redis 服务器
   $redis = new Redis(); 
   $redis->connect('127.0.0.1', 6379); 
   echo "Connection to server sucessfully"; 
   //在redis字符串中设置数据
   $redis->set("tutorial-name", "Redis tutorial"); 
   //获取存储的数据并打印
   echo "Stored string in redis:: " .$redisget("tutorial-name"); 
?>

执行上述程序时,将产生以下输出。

链接:https://www.learnfk.comhttps://www.learnfk.com/redis/redis-php.html

来源:LearnFk无涯教程网

Connection to server sucessfully 
Stored string in redis:: Redis tutorial 

Redis PHP列表示例

<?php 
   //连接到本地主机上的 Redis 服务器
   $redis = new Redis(); 
   $redis->connect('127.0.0.1', 6379); 
   echo "Connection to server sucessfully"; 
   //将数据存储在redis列表中
   $redis->lpush("tutorial-list", "Redis"); 
   $redis->lpush("tutorial-list", "Mongodb"); 
   $redis->lpush("tutorial-list", "Mysql");  
   
   //获取存储的数据并打印
   $arList = $redis->lrange("tutorial-list", 0 ,5); 
   echo "Stored string in redis:: "; 
   print_r($arList); 
?>

执行上述程序时,将产生以下输出。

链接:https://www.learnfk.comhttps://www.learnfk.com/redis/redis-php.html

来源:LearnFk无涯教程网

Connection to server sucessfully 
Stored string in redis:: 
Redis 
Mongodb 
Mysql 

Redis PHP键(key)示例

<?php 
   //连接到本地主机上的 Redis 服务器
   $redis = new Redis(); 
   $redis->connect('127.0.0.1', 6379); 
   echo "Connection to server sucessfully"; 
   //获取存储的密钥并打印
   $arList = $redis->keys("*"); 
   echo "Stored keys in redis:: " 
   print_r($arList); 
?>

执行该程序时,将产生以下输出。

Connection to server sucessfully 
Stored string in redis:: 
tutorial-name 
tutorial-list 

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

从0开始学大数据 -〔李智慧〕

消息队列高手课 -〔李玥〕

网络编程实战 -〔盛延敏〕

Swift核心技术与实战 -〔张杰〕

图解 Google V8 -〔李兵〕

WebAssembly入门课 -〔于航〕

去无方向的信 -〔小麥〕

徐昊 · TDD项目实战70讲 -〔徐昊〕

手把手带你搭建推荐系统 -〔黄鸿波〕

好记忆不如烂笔头。留下您的足迹吧 :)