首页 > php教程 > php手册 > 正文

Redis PHP连接操作,redisphp连接

php中文网
发布: 2016-07-06 14:25:24
原创
1757人浏览过

Redis PHP连接操作,redisphp连接

安装

要在php程序中使用redis,首先需要确保 redis 的php驱动程序和 php 安装设置在机器上。可以查看 php教程 教你如何在机器上安装php。现在,让我们来看看一下如何设置 redis 的php驱动程序。

需要从 github 上资料库: https://github.com/nicolasff/phpredis 下载 phpredis。下载完成以后,将文件解压缩到 phpredis 目录。在 Ubuntu 上安装这个扩展,可使用如下图所示的命令来安装。

<span class="pln">
cd phpredis
sudo phpize
sudo ./configure
sudo make
sudo make install

</span>
登录后复制

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

<span class="pln">
extension = redis.so

</span>
登录后复制

现在 Redis 和 PHP 安装完成。

连接到Redis服务器

<span class="pln">
<?php
   //Connecting to Redis server on localhost
   $redis = new Redis();
   $redis->connect('127.0.0.1', 6379);
   echo "Connection to server sucessfully";
   //check whether server is running or not
   echo "Server is running: " . $redis->ping();
?>

</span>
登录后复制

当执行程序时,会产生下面的结果:

码哩写作
码哩写作

最懂作者的AI辅助创作工具

码哩写作 60
查看详情 码哩写作

立即学习PHP免费学习笔记(深入)”;

<span class="pln">
Connection to server sucessfully
Server is running: PONG

</span>
登录后复制

Redis的PHP字符串实例

<span class="pln">
<?php
   //Connecting to Redis server on localhost
   $redis = new Redis();
   $redis->connect('127.0.0.1', 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");
?>

</span>
登录后复制

当执行程序时,会产生下面的结果:

立即学习PHP免费学习笔记(深入)”;

<span class="pln">
Connection to server sucessfully
Stored string in redis:: Redis tutorial

</span>
登录后复制

Redis的PHP列表示例

<span class="pln">
<?php
   //Connecting to Redis server on localhost
   $redis = new Redis();
   $redis->connect('127.0.0.1', 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);
?>

</span>
登录后复制

当执行程序时,会产生下面的结果:

立即学习PHP免费学习笔记(深入)”;

<span class="pln">
Connection to server sucessfully
Stored string in redis::
Redis
Mongodb
Mysql

</span>
登录后复制

Redis的PHP键例

<span class="pln">
<?php
   //Connecting to Redis server on localhost
   $redis = new Redis();
   $redis->connect('127.0.0.1', 6379);
   echo "Connection to server sucessfully";
   // Get the stored keys and print it
   $arList = $redis->keys("*");
   echo "Stored keys in redis:: "
   print_r($arList);
?>

</span>
登录后复制

当执行程序时,会产生下面的结果:

立即学习PHP免费学习笔记(深入)”;

<span class="pln">
Connection to server sucessfully
Stored string in redis::
tutorial-name
tutorial-list</span>
登录后复制
相关标签:
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

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