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