php中将文本数据库转为mysql数据库
发布:smiling 来源: PHP粉丝网 添加日期:2014-09-10 21:21:38 浏览: 评论:0
使用文本数据库一般是针对小型的应用程序了,这种不就需要安装数据库了,我们可以直接操作文本文件就可以了,但时怎么操作都不如mysql数据库好用了,下面我来给大家介绍一个文本数据库转为mysql数据库实例,代码如下:
- <?php
- require_once 'config.php';
- $action = emptyempty($_GET['action']) ? '' : $_GET['action'];
- $id = isset($_GET['id']) ? $_GET['id'] : 0;
- if (emptyempty($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 = "<a href="?page=$prev">上一页</a>";
- $goNext = "<a href="?page=$next">下一页</a>";
- 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) ? "<a href="?page=$i">$i</a>" : $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<a href="javascript:history.go(-1);">返回</a>";
- } 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<a href="javascript:history.go(-1);">返回</a>"; 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<a href="javascript:history.go(-1);">返回</a>"; 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 = "<a href="?action=vcat&cid=$cid&page=$prev">上一页</a>";
- $goNext = "<a href="?action=vcat&cid=$cid&page=$next">下一页</a>";
- 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) ? "<a href="?action=vcat&cid=$cid&page=$i">$i</a>" : $i;
- $pagelinks .= ($i < $pageall) ? '-' : null;
- } //开源代码phpfensi.com
- $data = array();
- while ($vclist = mysql_fetch_array($revcat)) {
- $data[] = $vclist;
- }
- include template('cplist');
- }
- mysql_close($sql);
题外话了,一个access转换成mysql数据库的实例.
Tags: php文本数据库 mysql数据库
相关文章
- ·xampp开启xampp Mysql数据库等的远程访问例子(2014-06-20)
- ·Php中使用PDO操作MySQL数据库(查询 更新 删除)(2014-09-10)
- ·php导入excel文件入mysql数据库例子(2014-09-10)
- ·PHP操作MySQL数据库的常用方法(2014-09-10)
- ·PHP把excel(xls)文件导入mysql数据库(2014-09-10)
- ·利用php把mysql数据库备份导出成sql示例(2014-09-10)
- ·PHP将mysql数据库导出为excel表(2014-09-10)
- ·PHP Mysql数据库备份类程序总结(2014-09-10)
- ·php 导出excel数据实例代码(2014-09-10)
- ·PHPExcel读取excel并导入mysql数据库代码(2014-09-10)
- ·PHPExcel读取excel并导入数据库(2014-09-10)
- ·php中实现mysql数据库备份与linux自动定时备份代码(2014-09-11)
- ·php 导入excel文件mysql数据库方法(2014-09-11)
- ·php导出mysql数据库中为excel代码(2014-09-11)
- ·php把csv文件导入到mysql数据库(2014-09-11)
- ·php access 数据库转换到mysql数据库代码(2014-09-11)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)