登录  /  注册

PHP链接redis的方法代码

小云云
发布: 2018-03-07 09:08:09
原创
3375人浏览过

在php程序中使用redis之前,需要确保在机器上安装了redis的php驱动程序和php环境。可以先在将php电脑上并配置好环境。本文就和大家分享php链接redis的方法代码,希望能帮助到大家。

安装

现在,让我们看看如何设置Redis PHP驱动程序。
从github库下载phpredis=> http://github.com/nicolasff/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 
   //Connecting to Redis server on localhost 
   $redis = new Redis(); 
   $redis->connect(&#39;127.0.0.1&#39;, 6379); 
   echo "Connection to server sucessfully"; 
   //check whether server is running or not 
   echo "Server is running: ".$redis->ping(); ?>
登录后复制

PHP

当程序执行时,将产生以下结果。

Connection to server sucessfully 
Server is running: PONG
登录后复制

Redis PHP字符串示例

<?php 
   //Connecting to Redis server on localhost 
   $redis = new Redis(); 
   $redis->connect(&#39;127.0.0.1&#39;, 6379); 
   echo "Connection to server sucessfully"; 
   //set the data in redis string 
   $redis->set("tutorial-name", "Redis tutorial"); 
   // Get the stored data and print it 
   echo "Stored string in redis:: " .$redis→get("tutorial-name"); ?>
登录后复制

执行上面代码,将生成以下结果 -

Connection to server sucessfully 
Stored string in redis:: Redis tutorial
登录后复制

Redis php列表示例

<?php 
   //Connecting to Redis server on localhost 
   $redis = new Redis(); 
   $redis->connect(&#39;127.0.0.1&#39;, 6379); 
   echo "Connection to server sucessfully"; 
   //store data in redis list 
   $redis->lpush("tutorial-list", "Redis"); 
   $redis->lpush("tutorial-list", "Mongodb"); 
   $redis->lpush("tutorial-list", "Mysql");  

   // Get the stored data and print it 
   $arList = $redis->lrange("tutorial-list", 0 ,5); 
   echo "Stored string in redis:: "; 
   print_r($arList); ?>
登录后复制

执行上面代码,将生成以下结果 -

Connection to server sucessfully 
Stored string in redis:: Redis 
Mongodb 
Mysql
登录后复制

Redis php键示例

<?php 
   //Connecting to Redis server on localhost 
   $redis = new Redis(); 
   $redis->connect(&#39;127.0.0.1&#39;, 6379); 
   echo "Connection to server sucessfully"; 
   // Get the stored keys and print it 
   $arList = $redis->keys("*"); 
   echo "Stored keys in redis:: " 
   print_r($arList); ?>
登录后复制

执行上面代码,将生成以下结果 -

Connection to server sucessfully 
Stored string in redis:: tutorial-name 
tutorial-list
登录后复制

相关推荐:

PHP操作Redis实例讲解

一个php+redis的简单实例分享

Redis在PHP的一些使用方法

以上就是PHP链接redis的方法代码的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号