zoukankan      html  css  js  c++  java
  • 展示数据库中的帖子内容

    从数据库中进行查询和列出表中内容

    下图是数据表user_fatie的结构

    下图是数据表user_fatie 的内容

    第一个页面是list.php。展示数据表user_fatie中的内容

     1 <!DOCTYPE html>
     2 <html>
     3     <head>
     4         <meta charset="UTF-8">
     5         <title>
     6             帖子列表
     7         </title>
     8     </head>
     9     <body>
    10         <div >
    11             文章列表
    12         </div>
    13     </body>
    14 </html>
    15 <?php
    16 require ("mysql_class.php");
    17 $db = new Mysql("localhost", "root", "201122", "userdb");
    18 define("TABLENAME", "user_fatie");
    19 $select = $db -> selectsql(TABLENAME);//查询数据库里的文章内容,该函数具体内容看mysql_class.php
    20 $num = $db -> num($select);//查询数据库里有几条文章列表,该函数具体内容看mysql_class.php
    21 echo "<table border=1>";
    22 for ($i = 0; $i < $num; $i++) {
    23     $row = $db -> arr($select);//mysql_fetch_array($result)从结果集中取得一行作为关联数组,或数字数组,或二者兼有返回根据从结果集取得的行生成的数组,如果没有更多行则返回 false。
    24     $id = $row['id'];//获取表中的id
    25     $title = $row['title'];//获取表中的title
    26     $aritle = $row['aritle'];//获取表中的aritle
    27     //a标签传值
    28     echo '<a  href="aritle.php?id=' . $id . '"  >
    29                 <p>' . $title . '</p>
    30             </a>';
    31 }
    32 echo "</table>";
    33 ?>

    第二个页面是aritle.php,展示单个的文章

     1 <!DOCTYPE html>
     2 <html>
     3     <head>
     4         <meta charset="UTF-8">
     5         <title>
     6             文章
     7         </title>
     8     </head>
     9     <body>
    10     </body>
    11 </html>
    12 <?php
    13 //$id=$_GET['id'];
    14 require ("mysql_class.php");
    15 $db = new Mysql("localhost", "root", "201122", "userdb");
    16 $id1 = intval($_GET['id']);//get上个页面list.php里<a>标签里的id值
    17 define("TABLENAME", "user_fatie");//sql有语句常量化
    18 $select = $db -> selectsql(TABLENAME);
    19 $num = $db -> num($select);
    20 for ($i = 0; $i < $num; $i++) {
    21     $row = $db -> arr($select);
    22     $id = $row['id'];
    23     $title = $row['title'];
    24     $aritle = $row['aritle'];
    25     if ($id == $id1) {//根据id获取文章内容
    26         echo '<h1>' . $title . '</h1>
    27              <p>' . $aritle . '</p>';
    28     }
    29 
    30 }
    31 ?>

    第三个页面数据库文件 mysql_class.php

     1 <?php
     2 header("content-type:text/html;charset=utf-8");
     3 class Mysql {
     4     private $host;
     5     //服务器地址
     6     private $root;
     7     //用户名
     8     private $password;
     9     //密码
    10     private $database;
    11     //数据库名
    12 
    13     //通过构造函数初始化类
    14     function Mysql($host, $root, $password, $database) {
    15         $this -> host = $host;
    16         $this -> root = $root;
    17         $this -> password = $password;
    18         $this -> database = $database;
    19         $this -> connect();
    20     }
    21 
    22     function connect() {
    23         $this -> conn = mysql_connect($this -> host, $this -> root, $this -> password);
    24 //        if($this->conn){
    25 //            echo "连接mysql成功";
    26 //        }else{
    27 //            echo "连接mysql失败";
    28 //        }
    29 //        $this->conn=
    30         mysql_select_db($this -> database, $this -> conn);
    31 //        if($this->conn){
    32 //            echo "连接db成功";
    33 //        }else{
    34 //            echo "连接db失败";
    35 //        }
    36         mysql_query("set names utf8");
    37     }
    38 
    39     function dbClose() {
    40         mysql_close($this -> conn);
    41     }
    42 
    43     function query($sql) {
    44         return mysql_query($sql);
    45     }
    46     function row($result) {
    47         return mysql_fetch_row($result);
    48 
    49     }
    50     
    51     function arr($result){
    52         return mysql_fetch_array($result);
    53     }
    54      function num($result){
    55          return mysql_num_rows($result);
    56      }
    57     
    58     function select($tableName, $condition) {
    59         return $this -> query("SELECT COUNT(*) FROM $tableName $condition");
    60     }
    61     function selectsql($tableName) {
    62         return $this -> query("SELECT * FROM $tableName");
    63     }
    64     function insert($tableName, $fields, $value) {
    65         $this -> query("INSERT INTO $tableName $fields VALUES$value");
    66     }
    67 }
    68 
    69 ?>

    结果图

  • 相关阅读:
    javascript ajax 脚本跨域调用全解析
    [转载]linux sed命令详解
    perl随记(1)
    TCL随记(2)
    TCL随记(1)
    C Shell 中的特殊变量
    异步FIFO为什么用格雷码
    VMM学习-vmm_log
    UVM基础之---Command-line Processor
    Verification Mind Games---how to think like a verifier像验证工程师一样思考
  • 原文地址:https://www.cnblogs.com/tanyongli/p/7350642.html
Copyright © 2011-2022 走看看