1. 程式人生 > >展示數據庫中的帖子內容

展示數據庫中的帖子內容

== for ++ data res charset condition connect use

從數據庫中進行查詢和列出表中內容

下圖是數據表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 ?>

結果圖

技術分享

技術分享

展示數據庫中的帖子內容