PHP Backup Database code (generate Word,excel,json,xml,sql)

Source: Internet
Author: User
Tags explode fread php class import database

Single-table backup
Code:
Copy CodeThe code is as follows:
<?php class Db {var $conn; function Db ($host = "localhost", $user = "root", $pass = "root", $db = "Test") {if (! $this->conn=mysql_connect ($ho          St, $user, $pass)) die ("can ' t connect to MySQL sever");          mysql_select_db ($db, $this->conn);        mysql_query ("SET NAMES ' UTF-8 '");        } function Execute ($sql) {return mysql_query ($sql, $this->conn);            } function Findcount ($sql) {$result = $this->execute ($sql);        Return mysql_num_rows ($result);            } function Findbysql ($sql) {$array =array ();            $result =mysql_query ($sql);            $i = 0;            while ($row =mysql_fetch_assoc ($result)) {$array [$i]= $row;               $i + +;        } return $array; }//$con several cases//null: Returns all records//array:eg. The array (' id ' = ' 1 ') returns the Id=1 record//string:eg. ' Id=1 ' returns Id=1 's record function ToEXtjson ($table, $start = "0", $limit = "Ten", $cons = "") {$sql = $this->generatesql ($table, $cons);           $totalNum = $this->findcount ($sql); $result = $this->findbysql ($sql. "           LIMIT ". $start.", ". $limit);          $resultNum = count ($result);//Current result number $str = "";          $str. = "{";          $str. = "' TotalCount ': ' $totalNum ',";          $str. = "' Rows ':";          $str. = "[";            for ($i =0; $i < $resultNum; $i + +) {$str. = "{";           $count =count ($result [$i]);           $j = 1; foreach ($result [$i] as $key = = $val) {if ($j < $count) {$str. = "'". $key. "': '". $v           Al. "',";           } elseif ($j = = $count) {$str. = "'". $key. "': '". $val. "'";}                    $j + +;           } $str. = "}";                 if ($i! = $resultNum-1) {$str. = ",";          }} $str. = "]";          $str. = "}";          return $str;    }    function GenerateSQL ($table, $cons) {$sql = "";//sql condition $sql = "SELECT * from". $table;             if ($cons! = "") {if (Is_array ($cons)) {$k = 0;          foreach ($cons as $key = + $val) {if ($k ==0) {$sql. = "where";          $sql. = $key;          $sql. = "' = '";          $sql. = $val. "'";          }else {$sql. = "and";          $sql. = $key;          $sql. = "' = '";          $sql. = $val. "'";          } $k + +;           }}else {$sql. = "where". $cons;        }} return $sql; } function Toextxml ($table, $start = "0", $limit = "Ten", $cons = "") {$sql = $this->generatesql ($table, $c           ONS);           $totalNum = $this->findcount ($sql); $result = $this->findbysql ($sql. "           LIMIT ". $start.", ". $limit);      $resultNum = count ($result);//Current result number header ("Content-type:text/xml");     $xml = "<?xml version=\" 1.0\ "encoding=\" utf-8\ "? >\n";           $xml. = "<xml>\n"; $xml. = "\t<totalcount>". $totalNum. "           </totalcount>\n ";           $xml. = "\t<items>\n";           for ($i =0; $i < $resultNum; $i + +) {$xml. = "\t\t<item>\n"; foreach ($result [$i] as $key = = $val) $xml. = "\t\t\t<". $key. " > ". $val." </". $key."           >\n ";           $xml. = "\t\t</item>\n";            } $xml. = "\t</items>\n";            $xml. = "</xml>\n";        return $xml; }//Output Word table function Toword ($table, $mapping, $fileName) {header (' Content-type:application               /doc '); Header (' content-disposition:attachment; Filename= '. $fileName. '.               Doc "'); Echo ' 

  

PHP Backup Database code (generate Word,excel,json,xml,sql)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.