2017-08-24 87 views
0

我在MySQL DB中有一個表,我想將它導出到excel。我已經嘗試了很多的源代碼,但它似乎沒有爲我工作;它看起來像我有一個連接問題,它應該在我的.php代碼和DB之間。我使用XMPP來託管我的數據庫(phpmyadmin)我可以通過輸入localhost/phpmyadmin或localhost來訪問它:80/phpmyadmin我創建到數據庫的一個名爲「test」,第二個是「gfdepot 「。我對第二個感興趣。在「gfdepot」數據庫中,我有6個表,並且我對名爲「zone」的表感興趣。導出MySQL DB到excel(連接問題)

那麼,目前我正在使用eclipse IDE(安裝了.php擴展名)來編寫我的網站腳本。我只想要一個可點擊的按鈕來將「區域」表導出到Excel文件。可點擊的按鈕效果不錯,但「.php」頁面不起作用! 無論我嘗試我總是得到連接錯誤。 目前,我嘗試下面的代碼。

<?php 
 
/*******EDIT LINES 3-8*******/ 
 
$DB_Server = "localhost"; //MySQL Server  
 
$DB_Username = "root"; //MySQL Username  
 
$DB_Password = "";    //MySQL Password  
 
$DB_DBName = "gfdepot";   //MySQL Database Name 
 
$DB_TBLName = "zone"; //MySQL Table Name 
 
$filename = "excelfilename";   //File Name 
 
/*******YOU DO NOT NEED TO EDIT ANYTHING BELOW THIS LINE*******/  
 
//create MySQL connection 
 
$sql = "Select * from $DB_TBLName"; 
 
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . mysql_errno()); 
 
//select database 
 
$Db = @mysql_select_db($DB_DBName, $Connect) or die("Couldn't select database:<br>" . mysql_error(). "<br>" . mysql_errno()); 
 
//execute query 
 
$result = @mysql_query($sql,$Connect) or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno());  
 
$file_ending = "xls"; 
 
//header info for browser 
 
header("Content-Type: application/xls");  
 
header("Content-Disposition: attachment; filename=$filename.xls"); 
 
header("Pragma: no-cache"); 
 
header("Expires: 0"); 
 
/*******Start of Formatting for Excel*******/ 
 
//define separator (defines columns in excel & tabs in word) 
 
$sep = "\t"; //tabbed character 
 
//start of printing column names as names of MySQL fields 
 
for ($i = 0; $i < mysql_num_fields($result); $i++) { 
 
echo mysql_field_name($result,$i) . "\t"; 
 
} 
 
print("\n");  
 
//end of printing column names 
 
//start while loop to get data 
 
    while($row = mysql_fetch_row($result)) 
 
    { 
 
     $schema_insert = ""; 
 
     for($j=0; $j<mysql_num_fields($result);$j++) 
 
     { 
 
      if(!isset($row[$j])) 
 
       $schema_insert .= "NULL".$sep; 
 
      elseif ($row[$j] != "") 
 
       $schema_insert .= "$row[$j]".$sep; 
 
      else 
 
       $schema_insert .= "".$sep; 
 
     } 
 
     $schema_insert = str_replace($sep."$", "", $schema_insert); 
 
     $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert); 
 
     $schema_insert .= "\t"; 
 
     print(trim($schema_insert)); 
 
     print "\n"; 
 
    } 
 
?>

,這是我所得到的。

" . mysql_error() . " 
 
" . mysql_errno()); //select database $Db = @mysql_select_db($DB_DBName, $Connect) or die("Couldn't select database: 
 
" . mysql_error(). " 
 
" . mysql_errno()); //execute query $result = @mysql_query($sql,$Connect) or die("Couldn't execute query: 
 
" . mysql_error(). " 
 
" . mysql_errno()); $file_ending = "xls"; //header info for browser header("Content-Type: application/xls"); header("Content-Disposition: attachment; filename=$filename.xls"); header("Pragma: no-cache"); header("Expires: 0"); /*******Start of Formatting for Excel*******/ //define separator (defines columns in excel & tabs in word) $sep = "\t"; //tabbed character //start of printing column names as names of MySQL fields for ($i = 0; $i < mysql_num_fields($result); $i++) { echo mysql_field_name($result,$i) . "\t"; } print("\n"); //end of printing column names //start while loop to get data while($row = mysql_fetch_row($result)) { $schema_insert = ""; for($j=0; $j

嗯,這是我在這裏的第一篇文章,我又不是編程高手,這是我第一次嘗試以.php代碼,TNX提前

+0

我想補充一點,我已經成功地展現出的.jsp頁面 在我的桌子,這是連接的線 康恩=的DriverManager.getConnection(「JDBC:MySQL的://本地主機:3306/gfdepot「,」root「,」「); – Aksel2099

回答

0

切換您的主機名指定端口號

例如。本地主機:3306

+0

1-在xampp上顯示3306爲MySQL, 2-我在eclipseIDE的tomcat服務器7.0(端口:8070)上託管.php文件,phpmyadmin在xampp上,xampp的Apache Serever端口是80,MySQL的端口是3306,所以我不認爲他們在同一臺服務器上。 3-well我已經在eclipse上使用該數據庫登錄並從數據庫中讀取信息,這就是爲什麼我認爲它接受來自外部源的連接 – Aksel2099

+0

您是否嘗試過更改主機名並將3306添加到您的php主機名變量? –

+0

是的,我已經將它更改爲$ DB_Server =「localhost:3306」; 但我得到同樣的錯誤,請檢查上面我編輯我的帖子,並添加我得到的錯誤信息。 – Aksel2099