php - Exporting MySQL DB to excel (Connection probleme) -
i have table in mysql db , want export excel. i've tried lot of source-codes seems nothing work me; looks i'm having connection problem , should between .php code , db.
first, i'm using xmpp host db(phpmyadmin) can access typing localhost/phpmyadmin or localhost:80/phpmyadmin i've created db 1 called "test" , second "gfdepot".well m interested in second one. in "gfdepot" db i've 6 tables, , i'm interested in table named "zone."
well, i'm using eclipse ide(with .php extension installed) script website. want have clickable button export "zone" table excel file. clickable button works well, ".php" page doesn't work! whatever try connection error. currently, m trying code below.
<?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 not need edit below line*******/ //create mysql connection $sql = "select * $db_tblname"; $connect = @mysql_connect($db_server, $db_username, $db_password) or die("couldn't connect 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 browser header("content-type: application/xls"); header("content-disposition: attachment; filename=$filename.xls"); header("pragma: no-cache"); header("expires: 0"); /*******start of formatting excel*******/ //define separator (defines columns in excel & tabs in word) $sep = "\t"; //tabbed character //start of printing column names names of mysql fields ($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 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"; } ?>
and get.
" . 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 browser header("content-type: application/xls"); header("content-disposition: attachment; filename=$filename.xls"); header("pragma: no-cache"); header("expires: 0"); /*******start of formatting excel*******/ //define separator (defines columns in excel & tabs in word) $sep = "\t"; //tabbed character //start of printing column names names of mysql fields ($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 data while($row = mysql_fetch_row($result)) { $schema_insert = ""; for($j=0; $j
well first post here , 'm not programming expert , it's first time trying .php code, , tnx in advance
switch hostname specify port number
eg. localhost:3306
wiki
Comments
Post a Comment