MySQL database export for Excel (ODBC driver)

Source: Internet
Author: User
Tags mysql host
MySQL database exported to Excel (ODBC driver)
PHP Code
  
  Currdir = DirName (__file__); }/* Gets the MySQL column name */Public Function Getsrccolumn () {$handle = @mysql_connect ($this->ho            St, $this->username, $this->pwd);            $this->srccolumn = Array (); if (Is_bool ($handle)) {echo "====**====
"; echo "MySQL connection error.
File: ". (__file__). "
Line number: ". (__line__). "
Error: ". Mysql_error (); echo "
====**====
"; Exit (0); } $ret = @mysql_select_db ($this->dbname, $handle); if ($handle = = FALSE) {echo "====**====
"; echo "MySQL Select database error.
File: ". (__file__). "
Line number: ". (__line__). "
Error: ". Mysql_error (); echo "
====**====
"; Exit (0); } $ret = @mysql_query (' describe '. $this->tbname, $handle); if (Is_bool ($ret)) {echo "====**====
"; echo "MySQL statement execution error.
File: ". (__file__). "
Line number: ". (__line__). "
Error: ". Mysql_error (); echo "
====**====
"; Exit (0); } while (($data = mysql_fetch_array ($ret)) = FALSE) {Array_push ($this->srccolumn, $data [' Field ']); } mysql_free_result ($ret); Mysql_close ($handle); return $this->srccolumn; }/* Gets the contents of the specified MySQL database table */Public Function Getsrcdata () {$handle = @mysql_connect ($this-& Gt;host, $this->username, $this->pwd); $this->srcdata = Array (); if (Is_bool ($handle)) {echo "====**====
"; echo "MySQL connection error.
File: ". (__file__). "
Line number: ". (__line__). "
Error: ". Mysql_error (); echo "
====**====
"; Exit (0); } $ret = @mysql_select_db ($this->dbname, $handle); if ($handle = = FALSE) {echo "====**====
"; echo "MySQL Select database error.
File: ". (__file__). "
Line number: ". (__line__). "
Error: ". Mysql_error (); echo "
====**====
"; Exit (0); } $ret = @mysql_query (' select * from '. $this->tbname, $handle); if (Is_bool ($ret)) {echo "====**====
"; echo "MySQL statement execution error.
File: ". (__file__). "
Line number: ". (__line__). "
Error: ". Mysql_error (); echo "
====**====
"; Exit (0); } while (($data = mysql_fetch_array ($ret)) = FALSE) {Array_push ($this->srcdata, $data); } mysql_free_result ($ret); Mysql_close ($handle); return $this->srcdata; }/* The main section exports the contents of the MySQL database as an Excel file */Public Function M2xls () {$handle = Odbc_connect ("Dri Ver={microsoft Excel Driver (*.xls)}; Readonly=false; Create_db=\ "". $this->xlsname. " \ ";D bq=". $this->xlsname, "", "" "); $sql = ' CREATE table '. $this->tbname. ' ('; foreach ($this->srccolumn as $value) {$sql. = ($value. "Text,"); } $sql = substr ($sql, 0,strlen ($sql)-1); $sql. = ")"; $ret = Odbc_exec ($handle, $sql); The above is to create an Excel file and transfer the MySQL column to the Excel file. foreach ($this->srcdata as $value) {$sql = ' insert into ['. $this->tbname. ' $] VALUES ('; for ($i =0; $i host = $host; $this->username = $username; $this->pwd = $pwd; $this->dbname = $dbName; $this->tbname = $tbName; }/* Set the name of the Excel database */Public Function Setxlsname ($xlsName) {$this->xlsname = $this-> ; Currdir. " \ \ ". $xlsName.". XLS "; return $this->xlsname; }/* The following section is a test function, you can delete the */header ("Content-type:text/html;charset=utf-8"); $mysql 2xls = new Mysql2xls (); $mysql 2xls->setmysqlargs (' localhost ', ' root ', ' ****** ', ' others ', ' Zhuifeng '); $mysql 2xls->setxlsname (' Test '); $mysql 2xls->getsrccolumn (); $mysql 2xls->getsrcdata (); $mysql 2xls->m2xls ();? >
  • Related Article

    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.