安裝
在PHP程序中使用Redis,需要確保我們有Redis的PHP驅(qū)動(dòng)程序和PHP安裝設(shè)置在機(jī)器上??梢圆榭碢HP教程教你如何在機(jī)器上安裝PHP?,F(xiàn)在,讓我們來(lái)看看一下如何設(shè)置Redis的PHP驅(qū)動(dòng)程序。
需要從github上資料庫(kù)https://github.com/nicolasff/phpredis下載phpredis。下載了它以后,將文件解壓縮到phpredis目錄。在Ubuntu上安裝這個(gè)擴(kuò)展,如下圖所示。
cd phpredis
sudo phpize
sudo ./configure
sudo make
sudo make install
現(xiàn)在,復(fù)制和粘貼“modules”文件夾的內(nèi)容復(fù)制到PHP擴(kuò)展目錄中,并在php.ini中添加以下幾行。
extension = redis.so
現(xiàn)在Redis和PHP安裝完成。
連接到Redis服務(wù)器
<?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();
?>
當(dāng)執(zhí)行程序時(shí),會(huì)產(chǎn)生下面的結(jié)果:
Connection to server sucessfully
Server is running: PONG
Redis的PHP字符串實(shí)例
<?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:: " + jedis.get("tutorial-name");
?>
當(dāng)執(zhí)行程序時(shí),會(huì)產(chǎn)生下面的結(jié)果:
Connection to server sucessfully
Stored string in redis:: Redis tutorial
Redis的PHP列表示例
<?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);
?>
當(dāng)執(zhí)行程序時(shí),會(huì)產(chǎn)生下面的結(jié)果:
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('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);
?>
當(dāng)執(zhí)行程序時(shí),會(huì)產(chǎn)生下面的結(jié)果:
Connection to server sucessfully
Stored string in redis::
tutorial-name
tutorial-list

創(chuàng)新互聯(lián)建站堅(jiān)信:善待客戶,將會(huì)成為終身客戶。我們能堅(jiān)持多年,是因?yàn)槲覀円恢笨芍档眯刨?。我們從不忽悠初訪客戶,我們用心做好本職工作,不忘初心,方得始終。十余年網(wǎng)站建設(shè)經(jīng)驗(yàn)
創(chuàng)新互聯(lián)建站是成都老牌網(wǎng)站營(yíng)銷(xiāo)服務(wù)商,為您提供成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、網(wǎng)站設(shè)計(jì)、
HTML5、網(wǎng)站制作、
品牌網(wǎng)站建設(shè)、微信小程序服務(wù),給眾多知名企業(yè)提供過(guò)好品質(zhì)的建站服務(wù)。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
文章題目:redis連接php-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://chinadenli.net/article46/djgieg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開(kāi)發(fā)、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、網(wǎng)站制作、微信小程序、移動(dòng)網(wǎng)站建設(shè)、企業(yè)建站
廣告
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源:
創(chuàng)新互聯(lián)