11.2隨筆
mysql寫法
//mysql_connect("主機名","使用者名稱","密碼") 連結池
mysql_connect("127.0.0.1","root","root") or die("連結失敗");
mysql_select_db("bbscms");//選擇操作的資料庫名
//設定字符集編碼格式
mysql_query("set names utf8");
$res = mysql_query($sql); //執行sql語句
$row = mysql_fetch_assoc($res); //每執行一次得到一條結果集 返回的資料是陣列
mysqli寫法
//連結資料庫
$link = mysqli_connect("主機名","使用者名稱","密碼") 連結池
$link = mysqli_connect("127.0.0.1","root","root") or die("連結失敗");
mysqli_select_db(連結池,"要操作的資料庫名");
mysqli_select_db($link,"bbscms");//選擇操作的資料庫名
設定資料庫編碼格式
mysqli_set_charset(連結池,"編碼格式");
mysqli_set_charset($link,"utf8");
$res = mysqli_query(連結池,操作的sql語句)
$res = mysqli_query($link,$sql);//執行sql語句
$row = mysqli_fetch_assoc($res); //每執行一次得到一條結果集 返回的資料是陣列
$_SESSION[] $_COOKIE[] 臨時的會話
$_SESSION['username'] = 資料庫值
清除session
unset($_SESSION['username'])
文章管理
文章內容
id title content time
init.php : init :配置 config:配置
select * from `news` limit 偏移量(編號為0對應第一條資料),固定的條數
select * from `news` limit 0,5 //第一頁的資料 1,2,3,4,5
0,5 //第一頁 1,2,3,4,5
5,5 //第二頁 6,7,8,9,10
10,5 //第三頁 11,12,13,14,15
15,5 //第四頁 16,17,18
($curpage - 1) * pagesize
得到總條數語句
select count(*) from `news`
一 html檔案 設定資料庫表單 設定核心配置檔案
二 登入頁面 連線資料庫
三 首頁和新建表news表單 判斷是否有使用者記錄 查詢文章內容
四 退出
五 增
六 刪
七 改
自己的小練習:
一 html檔案 設定資料庫表單 設定核心配置檔案
<?php
//核心配置檔案
header("Content-Type:text/html;chaset=utf-8");
//(1):連結資料庫 (2中連結方式) (1)mysql (2)mysqli
//mysql_connect("主機名","使用者名稱","密碼") 連結池
//mysql_connect("127.0.0.1","root","root") or die("連結失敗");
$link = mysqli_connect("127.0.0.1","root","root") or die("連結失敗");
//mysql_select_db("xiexie");//選擇操作的資料庫名
mysqli_select_db($link,"xiexie");//選擇操作的資料庫名
//設定字符集編碼格式
//mysql_query("set names utf8");
mysqli_set_charset($link,"utf8");
?>
二 登入頁面 連線資料庫
<?php
//表單裡面的name值對應上資料裡面欄位名
//連結資料庫
require("x666/init.php");
session_start();//開啟會話
if(!empty($_POST))
{
$username = $_POST['username'];//獲取文字框輸入的使用者名稱
$password = $_POST['password'];//獲取文字框輸入的密碼
//操作sql語句
$sql = "select * from `admin` where `username`='{$username}' and `password`='{$password}'";
//echo ($sql);
//$res = mysql_query($sql); //執行sql語句
$res = mysqli_query($link,$sql);
//$row = mysql_fetch_assoc($res); //每執行一次得到一條結果集 返回的資料是陣列
$row = mysqli_fetch_assoc($res); //每執行一次得到一條結果集 返回的資料是陣列
//print_r($row);die;
//驗證驗證碼
if($_SESSION['code'] != md5(strtolower($_POST['code'])))
{
die("驗證碼不對");
}else if($row['username']=="" and $row['password']==""){
die("賬號密碼不得為空");
}else
//驗證賬號密碼
if($row['username']==$username and $row['password']==$password)
{
$_SESSION["username"]=$row['username'];
header("location:http://localhost/11.2/index.php"); //跳轉頁面
}else{
die("請輸入正確的賬號密碼");
}
}
include("03.html");//檢視載入
?>
三 首頁和新建表news表單 判斷是否有使用者記錄 查詢文章內容
<?php
require("x666/init.php"); //載入核心配置檔案
session_start();
//判斷是否有使用者記錄
if(empty($_SESSION["username"]))
{
header("location:http://localhost/11.2/11.2.php");
}
$pagesize = 5;//固定的條數
$curpage = !empty($_GET['p'])? $_GET['p']: 1;//得到當前使用者點選的值
$offsetpage = ($curpage - 1) * $pagesize; //計算偏移量
//查詢文章內容
$sql = "select * from `news` limit {$offsetpage},{$pagesize}";
$res = mysqli_query($link,$sql);
$data = array();//宣告一個空陣列
while($row = mysqli_fetch_assoc($res))
{
$data[] = $row;
}
//統計總條數 ceil(總條數 / 固定的條數) 得到總頁數
$sql = "select count(*) as count from `news`";
$res = mysqli_query($link,$sql);
$row = mysqli_fetch_assoc($res);
//print_r($row);die;
$pageNum = $row['count'];//得到總條數
$perpage = ceil($pageNum/$pagesize);//得到總頁數
//echo ($perpage);die;
$page_str = "";
$page_str .='<a href="index.php?p=1" title="First Page">« 首頁</a>';
$page_str .='<a href="index.php?p='.($curpage-1).'" title="Previous Page">« 上一頁</a> ';
for($i=1;$i<=$perpage;$i++)
{
$class = ($i==$curpage)? "number current":"number" ;
$page_str .='<a href="index.php?p='.$i.'" class=".$class." title="1">'.$i.'</a>';
}
$page_str .='<a href="index.php?p='.($curpage+1).'" title="Next Page">下一頁 »</a>';
$page_str .='<a href="index.php?p='.$perpage.'" title="Last Page">尾頁 »</a>';
include("header.html");
include("index.html");
include("footer.html");
?>
四 退出
<?php
session_start();
unset($_SESSION['username']);
header("location:http://localhost/11.2/11.2.php");
?>
五 增
<?php
require("x666/init.php"); //載入核心配置檔案
session_start();
if(!empty($_POST))
{
$title = $_POST['title'];//獲取文字框輸入的標題
$content = $_POST['content'];//獲取文字框輸入的內容
$time = date("y-m-d");//獲取當前的日期
//操作資料庫
$sql = "insert into `news` values ('','{$title}','{$content}','{$time}')";
//echo ($sql);die;
$res = mysqli_query($link,$sql);
header("location:http://localhost/11.2/index.php");
}
include("header.html");
include("add.html");
include("footer.html");
?>
六 刪
<?php
require("x666/init.php"); //載入核心配置檔案
if(!empty($_GET))
{
$id = $_GET['id'];
$sql = "delete from `news` where id={$id}";
//$sql = "delete from `news` where id=".$id;
$res = mysqli_query($link,$sql);
header("location:http://localhost/11.2/index.php");
}
七 改
?>
<?php
require("x666/init.php"); //載入核心配置檔案
session_start();
//從資料庫獲取內容
if(!empty($_GET))
{
$id = $_GET['id'];
$sql = "select * from `news` where id={$id}";
$res = mysqli_query($link,$sql);
$row = mysqli_fetch_assoc($res);
//print_r($row);die;
}
//再把修改的內容存到資料庫
if(!empty($_POST))
{
$id = $_POST['id'];
$title = $_POST['title']; //修改後的標題
$content = $_POST['content'];//修改後的內容
$time = date("y-m-d");
$sql = "update `news` set `title`='{$title}',`content`='{$content}',`time`='{$time}' where id={$id}";
//echo ($sql);die;
$res = mysqli_query($link,$sql);
//$row =mysqli_fetch_assoc($res);
header("location:http://localhost/11.2/index.php");
}
include("header.html");
include("update.html");
include("footer.html");
?>