redis与pdo

原创2019-08-15 14:51:3822
摘要:<?php//function get_article(){//    $conn=mysqli_connect("localhost","root","root","redis");//    $sql="select id,title,content,crea

<?php
//function get_article(){
//    $conn=mysqli_connect("localhost","root","root","redis");
//    $sql="select id,title,content,create_time from article where staus='0' order by create_time desc";
//    $result = mysqli_query($conn, $sql);
//    while($row[] = mysqli_fetch_assoc($result)) {
//        foreach($row as $value){
//            $value['comment_num']=get_comment($value['id']);
//        }
//        $meaasge[]=$value;
//    }
//    return $meaasge;
//}
//function get_comment($id){
//    $conn=mysqli_connect("localhost","root","root","redis");
//    $sql="select count(id) as comment_num from comment where article_id=$id";
//    $result = mysqli_query($conn, $sql);
//    $row = mysqli_fetch_assoc($result);
//    return $row['comment_num'];
//}
//function set_redis($name,$data){
//    $redis=new Redis();
//    $redis->connect('127.0.0.1',6379);
//    $json=json_encode($data);
//    $redis->set($name,$json);
//}
//function get_redis($name){
//    $redis=new Redis();
//    $redis->connect('127.0.0.1',6379);
//    return  $redis->get($name);
//}
//function del_redis($name){
//    $redis=new Redis();
//    $redis->connect('127.0.0.1',6379);
//    $redis->del($name);
//}

@include_once "../const.php";

//实例化redis
function redis_create()
{
   $redis = new Redis();
   $redis->connect("127.0.0.1", 6379);
   return $redis;
}

//新增文章操作
function add_article()
{
   $data = $_POST;
   $time = time();
//    var_dump($data['title']);
////    die();
   $pdo = new PDO("mysql:host=127.0.0.1;dbname=redis", 'root', 'root');
   $sql = "insert into news( title,content,create_time) values (:title,:content,:create_time) ";
   $stmt = $pdo->prepare($sql);
   $stmt->bindParam(':title', $data['title']);
   $stmt->bindParam(':content', $data['content']);
   $stmt->bindParam(':create_time', $time);
   $stmt->execute();
   if ($stmt->rowCount() > 0) {
       $redis = redis_create();
       $redis->delete(KEY_ARTICLE);
       echo json_encode(array("code" => 0, "msg" => "发布成功"));
   } else {
       echo json_encode(array("code" => 1, "msg" => "发布失败"));
   }
}

//主页获取数据操作
function get_article()
{
   $redis = redis_create();
   if ($redis->zRange(KEY_ARTICLE, 0, -1)) {
       $articles = [];
       $redis_articles = $redis->zRevRange(KEY_ARTICLE, 0, -1);
       foreach ($redis_articles as $redis_article) {
           $articles[] = json_decode($redis_article, true);
       }
       return $articles;
   } else {
       $pdo = new PDO("mysql:host=127.0.0.1;dbname=redis", 'root', 'root');
       $stmt = $pdo->prepare("select * from news where status = 0 ORDER BY id DESC ");
       $stmt->execute();
       $articles = $stmt->fetchAll(PDO::FETCH_ASSOC);
       foreach ($articles as $article) {
           $article_json = json_encode($article);
           $redis->zAdd(KEY_ARTICLE, $article['create_time'], $article_json);
       }
       return $articles;
   }
}

//获取文章详细内容
function get_detail()
{
   $create_time = $_GET["create_time"];
   $redis = redis_create();
   $data = $redis->zRangeByScore(KEY_ARTICLE, $create_time, $create_time);
   if ($data) {
       return json_decode($data[0], true);
   } else {
       $pdo = new PDO("mysql:host=127.0.0.1;dbname=redis", 'root', 'root');
       $sql = "select * from news where create_time = :create_time";
       $stmt = $pdo->prepare($sql);
       $stmt->bindParam(":create_time", $create_time);
       $stmt->execute();
       $data = $stmt->fetch(PDO::FETCH_ASSOC);
       return $data;
   }
}

///删除文章内容
function del_article()
{
   $id = $_POST["id"];
   $pdo = new PDO("mysql:host=127.0.0.1;dbname=redis", 'root', 'root');
   $sql = "update news set status = 1 where id  = {$id}";
   $stmt = $pdo->prepare($sql);
   $stmt->execute();
   if ($stmt->rowCount() > 0) {
       $redis = redis_create();
       $redis->del(KEY_ARTICLE);
       echo json_encode(['code'=>0,"msg"=>"删除成功"]);
   }else{
       echo json_encode(['code'=>1,"msg"=>"删除失败"]);
   }
}

//发布评论
function add_comment()
{
//    var_dump($_POST);
   $data = $_POST;
   $id = $data['id'];
   $comment = $data['comment'];
   $time = time();
   $redis = redis_create();
   $pdo = new PDO("mysql:host=127.0.0.1;dbname=redis", 'root', 'root');
   $sql = "insert into comments(article_id,comment,create_time) values (:article_id,:comment,:create_time)";
   $stmt = $pdo->prepare($sql);
   $stmt->bindParam(":article_id", $id);
   $stmt->bindParam(":comment", $comment);
   $stmt->bindParam(":create_time", $time);
   $stmt->execute();
   if ($stmt->rowCount() > 0) {
//        $data =["comment"=>$comment,"create_time"=>$time];
//        $content_json = json_encode($data);
       $redis->del(KEY_COMMENTS . $id);
       $redis->incr(KEY_COMMENTS_COUNT . $id);
       echo json_encode(array("code" => 0, "msg" => "评论成功"));
   } else {
       echo json_encode(array("code" => 1, "msg" => "评论失败"));
   }
}

//获取评论,id为文章id
function get_comment($id)
{
//    var_dump($id);
   $redis = redis_create();
   if ($redis->exists(KEY_COMMENTS . $id)) {
       $comments = [];
       $redis_comments = $redis->lRange(KEY_COMMENTS . $id, 0, -1);
       foreach ($redis_comments as $redis_comment) {
           $comments[] = json_decode($redis_comment, true);
       }
       return $comments;
   } else {
       $pdo = new PDO("mysql:host=127.0.0.1;dbname=redis", 'root', 'root');
       $stmt = $pdo->prepare("select * from comments where article_id = :id ORDER BY id DESC ");
       $stmt->bindParam(":id", $id);
       $stmt->execute();
       $comments = $stmt->fetchAll(PDO::FETCH_ASSOC);
       foreach ($comments as $comment) {
           $comment_json = json_encode($comment);
           $redis->lPush(KEY_COMMENTS . $id, $comment_json);
       }
       return $comments;
   }
}

//获取评论数量,id为文章id
function get_comment_count($id)
{
   $redis = redis_create();
   if ($redis->exists(KEY_COMMENTS_COUNT . $id)) {
       $count = $redis->get(KEY_COMMENTS_COUNT . $id);
   } else {
       $pdo = new PDO("mysql:host=127.0.0.1;dbname=redis", 'root', 'root');
       $sql = "select count(*) as 'c' from comments where article_id = {$id}";
       $stmt = $pdo->prepare($sql);
       $stmt->execute();
       $count = $stmt->fetch(PDO::FETCH_ASSOC);
       $redis->set(KEY_COMMENTS_COUNT . $id, $count['c']);
   }
   return $count;
}

?>

批改老师:Peter-Zhu批改时间:2019-08-16 09:47:51
老师总结:pdo操作数据库, 现在已是主流, 一定要好好学

发布手记

热门词条

第九期线上培训班