> php教程 > php手册 > 본문

Redis PHP连接操作,redisphp连接

WBOY
풀어 주다: 2016-07-06 14:25:24
원래의
1449명이 탐색했습니다.

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>
로그인 후 복사

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

<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>
로그인 후 복사

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

<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>
로그인 후 복사

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

<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>
로그인 후 복사

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

<span class="pln">
Connection to server sucessfully
Stored string in redis::
tutorial-name
tutorial-list</span>
로그인 후 복사
관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!