php中將文本資料庫轉為mysql資料庫_PHP教程

來源:互聯網
上載者:User
使用文本資料庫一般是針對小型的應用程式了,這種不就需要安裝資料庫了,我們可以直接操作文字檔就可以了,但時怎麼操作都不如mysql資料庫好用了,下面我來給大家介紹一個文本資料庫轉為mysql資料庫執行個體。

代碼如下 複製代碼
require_once 'config.php';

$action = empty($_GET['action']) ? '' : $_GET['action'];
$id = isset($_GET['id']) ? $_GET['id'] : 0;

if (empty($action)) {//列表

$result = "SELECT * FROM posts ORDER BY pid DESC";
$items = 3;
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$page = ($page =='' && $page < 0) ? 1 : $page;
$total = mysql_num_rows(mysql_query($result));//資料條目總數
$pageall = ceil($total/$items);
$offset = ($page-1)*$items;//資料開始位置
$result .= " limit {$offset},{$items}";//每頁顯示
$results = mysql_query($result);

$prev = $page - 1;
$next = $page + 1;
$goPrev = "上一頁";
$goNext = "下一頁";

if ($page == $pageall){ $goNext = NULL;}

$pagestate = '';

if ($page == 1 && $pageall > 1) {
$pagestate .= $goNext;
} elseif ($pageall > $page || $page >1) {
$pagestate .= $goNext . '|' .$goPrev;
} elseif ($page >1 ) {
$pagestate .= $goPrev;
} else {
$pagestate .= 'Just One Page';
}

$pagelinks = '';

for ($i = 1; $i <= $pageall; $i++) {
$pagelinks .= ($i != $page) ? "$i" : $i;
$pagelinks .= ($i < $pageall) ? '-' : null;
}

// end 分頁
$data = array();

//列出文章
while ($row = mysql_fetch_array($results)) {//mysql_fetch_array ; 從查詢結果取出一行作為數組;
$data[] = $row;//
}

//列出分類
$recat = mysql_query("SELECT * FROM category ORDER BY cid DESC");
$cdata = array();

while ($catlist = mysql_fetch_array($recat)) {
$cdata[$catlist['cid']] = $catlist;
}

include template('index');

} elseif ($action == 'post') {//發表

$title = $_POST['title'];
$date = date("Y-m-d H:i:s");
$content = $_POST['content'];
$cid = $_POST['cat'];

if ($title == '' || $content == '' || $cid == '') {

echo "缺少必填項nnnn返回";

} else {

$insertPost = "INSERT INTO posts (title,time,post,cid) VALUES ('$title','$date','$content','$cid')";

if (!mysql_query($insertPost,$sql)) {
die('ERROR: '. mysql_error());
}

header('location: index.php');

}

} elseif ($action == 'view') {//查看

$view = mysql_query("SELECT * FROM posts WHERE pid='$id'");

while ($row = mysql_fetch_array($view)) {

$title = $row['title'];
$time = $row['time'];
$post = $row['post'];

}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {//評論

$name = isset($_POST['name']) ? $_POST['name'] : 0;
$review = isset($_POST['review']) ? $_POST['review'] : 0;

if ($name == '' && $review == '') { echo "缺少必填項"; exit; }
$insertRview = "INSERT INTO review (pid,name,review) VALUES ('$id','$name','$review')";

if (!mysql_query($insertRview,$sql)) {
die('ERROR: ' . mysql_error());
}

header("location: ?action=view&id=$id");
}

$rert = mysql_query("SELECT * FROM review WHERE pid='$id' ORDER BY rid ASC");
$data = array();

while ($relist = mysql_fetch_array($rert)){

$data[] = $relist;

}

include template('view');

} elseif ($action == 'del') {//刪除

mysql_query("DELETE FROM posts WHERE pid='$id'");
header("location: index.php");

} elseif ($action == 'edit') {//編輯

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

$title = $_POST['title'];
$content = $_POST['content'];
$date = date("Y-m-d H:i:s");

if ($title == '' or $content == '') { echo "缺少必填項nnnn返回"; exit;}

mysql_query("UPDATE posts SET title='$title',time='$date',post='$content' WHERE pid='$id'");
header("location: index.php");

} else {

$row = mysql_query("SELECT * FROM posts WHERE pid='$id'");
$row = mysql_fetch_array($row);

include template('edit');

}

} elseif ($action == 'newcat') {//建立分類

$cat = isset($_POST['category']) ? $_POST['category'] : 0;

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

if ($cat == '') { echo "缺少必填項nnnn返回"; exit;}

$insertCat = "INSERT INTO category (category) VALUES ('$cat')";

if (!mysql_query($insertCat,$sql)){
die('ERROR: ' . mysql_error());
}

header("location: index.php");

}
include template('jioncat');

} elseif ($action == 'vcat') {

$cid = $_GET['cid'];

$revcats = "SELECT * FROM posts WHERE cid='$cid' ORDER BY cid DESC";
$items = 1;
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$page = ($page =='' && $page < 0) ? 1 : $page;
$total = mysql_num_rows(mysql_query($revcats));//資料條目總數
$pageall = ceil($total/$items);
$offset = ($page-1)*$items;//資料開始位置
$revcats .= " limit {$offset},{$items}";//每頁顯示
$revcat = mysql_query($revcats);

$prev = $page - 1;
$next = $page + 1;
$goPrev = "上一頁";
$goNext = "下一頁";

if ($page == $pageall){ $goNext = NULL;}

$pagestate = '';

if ($page == 1 && $pageall > 1) {
$pagestate .= $goNext;
} elseif ($pageall > $page || $page >1) {
$pagestate .= $goNext . '|' .$goPrev;
} elseif ($page >1 ) {
$pagestate .= $goPrev;
} else {
$pagestate .= 'Just One Page';
}

$pagelinks = '';

for ($i = 1; $i <= $pageall; $i++) {
$pagelinks .= ($i != $page) ? "$i" : $i;
$pagelinks .= ($i < $pageall) ? '-' : null;
}

$data = array();

while ($vclist = mysql_fetch_array($revcat)) {
$data[] = $vclist;
}

include template('cplist');

}

mysql_close($sql);

題外話了,一個access轉換成mysql資料庫的執行個體

點擊下載:http://file.bKjia.c0m/download/2013/05/14/db2mysql.zip

http://www.bkjia.com/PHPjc/630714.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/630714.htmlTechArticle使用文本資料庫一般是針對小型的應用程式了,這種不就需要安裝資料庫了,我們可以直接操作文字檔就可以了,但時怎麼操作都不如m...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.