安裝
在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
//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();
?>
當執行程序時,會產生下面的結果:
Connection to server sucessfully
Server is running: PONG
Redis的PHP字符串實例
<?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");
?>
當執行程序時,會產生下面的結果:
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);
?>
當執行程序時,會產生下面的結果:
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);
?>
當執行程序時,會產生下面的結果:
Connection to server sucessfully
Stored string in redis::
tutorial-name
tutorial-list

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