最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php+Memcached實現(xiàn)簡單留言板功能示例

 更新時間:2017年02月15日 08:54:45   作者:水晶依戀  
這篇文章主要介紹了php+Memcached實現(xiàn)簡單留言板功能,結(jié)合實例形式較為詳細的分析了php結(jié)合memcached實現(xiàn)留言板的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了php+Memcached實現(xiàn)簡單留言板功能。分享給大家供大家參考,具體如下:

MyPdo.php

<?php
class MyPdo{
  private $pdo;
  function __construct()
  {
    $this->pdo = $this->getPdo();
  }
   /**
   * CreatePDO
   *
   * @return PDO
   */
  public function getPdo()
  {
    $dbms='mysql';
    $dbName='testdb';
    $user='root';
    $pwd='diligentyang';
    $host='localhost';
    $dsn="$dbms:host=$host;dbname=$dbName";
    try{
      $pdo=new PDO($dsn,$user,$pwd);
    }catch(Exception $e){
      echo $e->getMessage().'<br>';
      exit();
    }
    $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    $pdo->exec("set names utf8");
    return $pdo;
  }
   /**
   * Execute SQL
   *
   * @param string $sql Sql
   * @param string $mode Mode
   *
   * @return mixed
   */
  function query($sql = "", $mode = "array")
  {
    $sql = trim($sql);
    if ($sql == "") {
      $this->showErrors("the mothe query neet at least one param!");
    }
    $query = $this->pdo->query($sql);
    if (!$query) {
      $this->showErrors("the sql string is false");
    }
    if (strpos(strtolower($sql), "select") ===false) {
      return $query;
    }
    switch ($mode) {
    case 'array' :
      $res = $query->fetchAll(PDO::FETCH_ASSOC);
      break;
    case 'object' :
      $res = $query->fetchObject();
      break;
    case 'count':
      $res = $query->rowCount();
      break;
    default:
      $this->showErrors("SQLERROR: please check your second param!");
    }
    return $res;
  }
  /**
  * 提示錯誤
  *
  * @param string $str 錯誤提示內(nèi)容
  */
  public function showErrors($str)
  {
    echo "<h1>$str<h1/>";
    exit();
  }
}

ShowMessage.php

<?php
include("MyPdo.php");
//連接Memcached服務(wù)器
$m = new Memcached();
$m->addServer('127.0.0.1',11211);
//獲取Memcached中的list
$res = $m->get("list");
//如果沒有數(shù)據(jù),則從數(shù)據(jù)庫中查出,并放入Memcached中,如果有數(shù)據(jù)則直接輸出
if(!$res){
  $MyPdo = new MyPdo();
  $res = $MyPdo->query("select * from message","array");
  $m->set('list',$res,3600);
}
foreach($res as $val){
  echo $val['title']."-------".$val['content']."<br>";
}
?>
<a href="AddMessage.php" rel="external nofollow" >添加留言</a>

AddMessage.php

<form action="CheckAdd.php" method="post">
  標題:<input type="text" name="title"><br>
  內(nèi)容:<input type="text" name="content"><br>
  <input type="submit" value="提交">
</form>

CheckAdd.php

<?php
include("MyPdo.php");
//連接Memcached服務(wù)器
$m = new Memcached();
$m->addServer('127.0.0.1',11211);
$title = $_POST['title'];
$content = $_POST['content'];
$MyPdo = new MyPdo();
$res = $MyPdo->query("insert into message(title,content) values('$title','$content')");
if($res){//如果insert語句執(zhí)行成功則清除Memcache中的緩存
  $m->delete("list");
}
header("location:ShowMessage.php");

運行結(jié)果如下所示:

注:此例子只是簡單實現(xiàn)了,留言列表和添加留言功能,需要注意的是,如果對數(shù)據(jù)庫的數(shù)據(jù)有了添加或修改,需要清除緩存,然后重新緩存一下,已保證數(shù)據(jù)顯示同步。

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP+MySQL留言板開發(fā)專題》、《php緩存技術(shù)總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP錯誤與異常處理方法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設(shè)計有所幫助。

相關(guān)文章

最新評論

徐水县| 桦甸市| 湖南省| 祁阳县| 呼伦贝尔市| 德格县| 钦州市| 扎兰屯市| 布尔津县| 天门市| 平凉市| 电白县| 山东省| 云南省| 汾西县| 虞城县| 合山市| 彰化市| 余姚市| 利津县| 永定县| 个旧市| 黄陵县| 洛川县| 林芝县| 潞城市| 沂南县| 肃宁县| 龙陵县| 色达县| 平江县| 泸西县| 志丹县| 仙游县| 临城县| 乌兰县| 和政县| 册亨县| 离岛区| 金门县| 台北县|