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 :
- 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.
- 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 -> 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
- Create File datatxt.php :
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,
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