Simple example of using redis in PHP _ PHP Tutorial

Source: Internet
Author: User
Simple example of using redis in PHP. Simple example of using redis in PHP this article mainly introduces the simple example of using redis in PHP. it mainly shows you how to use redis in php, for more information about using redis in PHP, see the simple example.

This article mainly introduces a simple example of using apsaradb for redis in PHP. it mainly shows you how to use apsaradb for redis in php.

The example is very simple. it is explained in the annotations, so there is not much nonsense here.

The code is as follows:


/* Obtain the database name from the platform */
$ Dbname = "";
/* Obtain host, port, user, pwd from environment variables */
$ Host = '';
$ Port = '';
$ User = '';
$ Pwd = '';
Try {
/* After the connection is established, perform auth verification before performing the set operation */
$ Redis = new Redis ();
$ Ret = $ redis-> connect ($ host, $ port );
If ($ ret = false ){
Die ($ redis-> getLastError ());
}
$ Ret = $ redis-> auth ($ user. "-". $ pwd. "-". $ dbname );
If ($ ret = false ){
Die ($ redis-> getLastError ());
}
/* Next, you can perform operations on the database. for detailed operation methods, see the official phpredis documentation */
$ Redis-> flushdb ();
$ Ret = $ redis-> set ("key", "value ");
If ($ ret = false ){
Die ($ redis-> getLastError ());
} Else {
Echo "OK". $ redis-> get ("key ");
}
} Catch (RedisException $ e ){
Die ("Uncaught exception". $ e-> getMessage ());
}
?>

The above is all the content of the code described in this article. I hope you will like it.

Http://www.bkjia.com/PHPjc/963977.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/963977.htmlTechArticlePHP using redis simple example to share this article mainly introduced the PHP using redis simple example to share, mainly to show you, php using redis method, there is a need for small partners reference...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.