In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
PHP memcache is used in database query to reduce the number of times to connect to the database and reduce the pressure on the server!
/ * * memcache application description: memory cache memory cache * how it works * server port port 11211 * MemCached access key value pair key = > value * 1. Access to the private network * 2. Set firewall * / / create memcache object $mem = new Memcache (); / / connect to the memcache server $host = 'localhost'; $port =' 11211 servers; $mem- > connect ($host,$port); / / $mem- > addserver ($host,$port); / / $mem- > addserver ('192.168.10.254 clients, 11211) / / add the memory of a server $dsn = 'mysql:dbname=test;host=localhost'; $username =' root'; $passwd = 'root' $options = array (PDO::ATTR_AUTOCOMMIT= > true, PDO::ATTR_PERSISTENT= > true) / / Connect to the database try {/ / create objects $pdo = new PDO ($dsn, $username, $passwd, $options); / / set the exception mode $pdo- > setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);} catch (PDOException $e) {echo "database connection failed:". $e-> getMessage () Exit;} $statement = "select * from users where id >? And id
< ?"; //键名用md5()加密后 名字更规范 防止查询语句信息泄漏 $key = md5($statement); //直接从内存要数据 $data = $mem->Get ($key); / / determine whether $data exists. If not, you need to connect to the database to get the data! The data is fetched directly from memory to realize caching. If (empty ($data)) {try {/ / to the database management system, compile and wait for no execution of $pdostatement=$pdo- > prepare ($statement); $pdostatement- > execute (array (3line 6)); / / set the result mode $pdostatement- > setFetchMode (PDO::FETCH_NUM) $data = $pdostatement- > fetchAll (PDO::FETCH_ASSOC); / / keep in cache $mem- > set ($key, $data,MEMCACHE_COMPRESSED,10); echo 'first database query!';} catch (PDOException $e) {echo 'error cause:'. $e-> getMessage () }} / / output echo'; foreach ($data as $value) {echo'; echo''. $value ['id'].'; echo'. $value ['username'].'; echo'. $value ['password'].'' Echo'. $value ['email'].'; echo'';} echo''; / / close the connection $mem- > close ()
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.