Home php教程 php手册 php中将文本数据库转为mysql数据库

php中将文本数据库转为mysql数据库

Jun 13, 2016 am 10:09 AM
mysql php generally No use app database text yes of

使用文本数据库一般是针对小型的应用程序了,这种不就需要安装数据库了,我们可以直接操作文本文件就可以了,但时怎么操作都不如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     $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         $pagelinks .= ($i != $page) ? "$i" : $i;
        $pagelinks .= ($i     }
 
  // 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     $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         $pagelinks .= ($i != $page) ? "$i" : $i;
        $pagelinks .= ($i     }
 
    $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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1677
14
PHP Tutorial
1280
29
C# Tutorial
1257
24
MySQL vs. phpMyAdmin: Understanding the Key Differences MySQL vs. phpMyAdmin: Understanding the Key Differences May 06, 2025 am 12:17 AM

MySQL is a database management system, and phpMyAdmin is a web tool for managing MySQL. 1.MySQL is used to store and manage data and supports SQL operations. 2.phpMyAdmin provides a graphical interface to simplify database management.

Navicat and MySQL: A Perfect Partnership Navicat and MySQL: A Perfect Partnership May 05, 2025 am 12:09 AM

Navicat and MySQL are perfect matches because they can improve database management and development efficiency. 1.Navicat simplifies MySQL operations and improves work efficiency through graphical interfaces and automatic generation of SQL statements. 2.Navicat supports multiple connection methods, which facilitates local and remote management. 3. It provides powerful data migration and synchronization capabilities, suitable for advanced usage. 4.Navicat helps with performance optimization and best practices such as regular backup and query optimization.

PHP performance optimization strategies. PHP performance optimization strategies. May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

MySQL: A Practical Application of SQL MySQL: A Practical Application of SQL May 08, 2025 am 12:12 AM

MySQL is popular because of its excellent performance and ease of use and maintenance. 1. Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2. Insert and query data: operate data through INSERTINTO and SELECT statements. 3. Optimize query: Use indexes and EXPLAIN statements to improve performance.

MySQL vs. Oracle: Licensing, Features, and Benefits MySQL vs. Oracle: Licensing, Features, and Benefits May 08, 2025 am 12:05 AM

The main difference between MySQL and Oracle is licenses, features, and advantages. 1. License: MySQL provides a GPL license for free use, and Oracle adopts a proprietary license, which is expensive. 2. Function: MySQL has simple functions and is suitable for web applications and small and medium-sized enterprises. Oracle has powerful functions and is suitable for large-scale data and complex businesses. 3. Advantages: MySQL is open source free, suitable for startups, and Oracle is reliable in performance, suitable for large enterprises.

Best Practices for Dependency Injection in PHP Best Practices for Dependency Injection in PHP May 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

phpMyAdmin's Function: Interacting with MySQL (SQL) phpMyAdmin's Function: Interacting with MySQL (SQL) May 07, 2025 am 12:16 AM

phpMyAdmin simplifies MySQL database management through the web interface. 1) Create databases and tables: Use graphical interface to operate easily. 2) Execute complex queries: such as JOIN query, implemented through SQL editor. 3) Optimization and best practices: including SQL query optimization, index management and data backup.

PHP Performance Optimization Checklist: Improve Speed Now PHP Performance Optimization Checklist: Improve Speed Now May 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

See all articles