Home > Backend Development > PHP Tutorial > Convert text database to mysql database in php_PHP tutorial

Convert text database to mysql database in php_PHP tutorial

WBOY
Release: 2016-07-13 17:06:26
Original
3935 people have browsed it

The use of text databases is generally for small applications. This does not require the installation of a database. We can directly operate text files, but no matter how we operate, it is not as easy to use as the mysql database. Let me explain below. Let me introduce an example of converting a text database into a mysql database.

The code is as follows Copy code
require_once 'config.php';

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

if (empty($action)) {//List

$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));//Total number of data entries
$pageall = ceil($total/$items);
$offset = ($page-1)*$items;//Data starting position
$result .= "limit {$offset},{$items}";//Display on each page
$results = mysql_query($result);

$prev = $page - 1;
$next = $page + 1;
$goPrev = "Previous page";
$goNext = "Next page";

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 paging
$data = array();

//List articles
While ($row = mysql_fetch_array($results)) {//mysql_fetch_array; Take out a row from the query result as an array;
         $data[] = $row;//
}

//List categories
$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') {//Publish

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

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

echo "Missing required fieldsnnnnReturn";

} 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));//Total number of data entries
$pageall = ceil($total/$items);
$offset = ($page-1)*$items;//Data starting position
$revcats .= "limit {$offset},{$items}";//Display per page
$revcat = mysql_query($revcats);

$prev = $page - 1;
$next = $page + 1;
$goPrev = "Previous page";
$goNext = "Next page";

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);

Off topic, an example of converting access into mysql database

Click to download: http://file.bKjia.c0m/download/2013/05/14/db2mysql.zip

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/630714.htmlTechArticleUsing text databases is generally for small applications. This does not require installing a database. We can directly It is enough to operate text files, but no matter how you operate it, it is not as good as m...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template