Jump to content

SQL Server 2005 to Excel via PHP


tvks

Recommended Posts

Hi all,

This following code can be used for transferring the contents of a Sql Server 2005 to CSV format using PHP.

[code]
set_time_limit(500000);
$sqlconnect=mssql_connect("host","username","password");
$sqldb=mssql_select_db("dbname",$sqlconnect);
$sqlquery="select * from table_name;";
$results= mssql_query($sqlquery);

$out = "";

while ($field = mssql_fetch_field($results)){
$out .= ' " '.$field->name.' ", ';
}
$out .="\n";

while ($row = mssql_fetch_assoc($results)){
foreach ($row as $col=>$val){
$out .= ' " '.$val.' ", ';

$out .="\n";
}
mssql_close($sqlconnect);

// Put all values from $out to export.csv.
fputs($f, $out);
fputs($f, $out);
fclose($f);

header('location:export.csv');

?>
[/code]

This code works if the table is a small. I would be happy if anybody can share me a better code for transferring a big Sql Server 2005 code to CSV using PHP. Others, feel free enjoying the code.

Do remember to change the hostname, username, password and database name along with the SQL query.

Regards,

tvks
Link to comment
https://forums.phpfreaks.com/topic/31504-sql-server-2005-to-excel-via-php/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.