Eksport SQL Data to Data.Txt Using PHP

Recently i have a project that need access data from SQL Server using PHP. i never do it before, because i always use MySQL to my database connection. This time my bos ask me to make a Website interface to access SQL data and eksport to Data.txt and Data.Dat.

A. The Connection between SQL and PHP

In my company luckly use Xampp 1.7.3 version, so i try to find some code to make connection between SQL Server and PHP, i got it in Stackoverflow forums. i will share with you (COnnection using Xampp 1.7.3)

if we have Xampp 1.7.3, its lucky because its included mssql Module. we dont need to download mssql anymore, just need to change the configuration in file PHP.ini :

  1. before start the configuration, ensure both of these file (php_mssql.dll & php_pdo_mssql.dll) is existed in directory : C:\xampp\php\ext\. (assumed xampp has been installed on drive C). for version 1.7.3 both of file must be existed.
  2. Open file PHP.ini in drive C:\xampp\php\ with notepad or ather app, find 2 below text and remove the sysmol (;) to activate the extension of configuration in PHP.

;extension=php_mssql.dll                        ->         extension=php_mssql.dll
;extension=php_pdo_mssql.dll            ->         

the configuration is finished, Just try to restart the Apache Service.

and create file Connection.php :


B. Eksport SQL Data to File.Txt or File.Dat

  1. Create File datatxt.php :

data txt

Eksplanation :

Code row 13 :

$query = “EXEC RptAttdLog ‘$_GET[tanggal1]’, ‘$_GET[tanggal2]'”;

My query Code is Procedure RptAttdLog, just replace with your own query, like (select * from tb_book) .

when we execute the Code, it will generate link Download our Data.Txt,

download txt

product Data.txt will shown like this picture :



If we want to Make another format data, like .Dat .Csv

Just change the below Code (row 29 & 30):

header(“Content-type: text”);    ————->>> change “Text” with your format like “Dat” / “Csv”
header(“Content-Disposition: attachment; filename=export.txt”); —>>> change “.Txt” with your format like “.Dat” / “.Csv”

Ok thanks to read my Post.

i hope it can helpful for you all.

if you have any question or problems, just comment or give me an email




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s