note 44407 added to ref.mssql
| From: | matthewsherwin at optusnet dot com dot au | Date: | Wed, 04 Aug 2004 22:13:27 +0000 |
| Subject: | note 44407 added to ref.mssql | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-73961@lists.php.net to get a copy of this message | ||
I am running Apache 2.0.49 trying to connect to a Mssql server. After spending many days searching
for a script to connect to mssql and run the query and export it to excel i found this script. It is
a combanation of a few scripts hybridised from mysql, but it worked for me.
<?
header("Content-type: application/vnd.ms-excel"); // Call the local application
header("Content-Disposition: attachment; filename=nameoffile.csv"); //What the
downloadable file will be called
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>MSSQL Database Query</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<table width="450" cellpadding="5" cellspacing="5"
border="1">
<?
// Your database details
$User = "username";
$Pass = "password";
$db = "database";
$link = mssql_connect( "server:port", $User, $Pass );
if ( ! $link) { die( " couldn't connect" ); }
//Name of your database, the [] brackets around the name is because this microsoft based database
allowed someone to have a space in the name. These brackets fix this problem.
mssql_select_db( "[$db]" );
//Query
$Result = mssql_query ("SELECT * from table");
//Now we count the rows
$Num_Rows = mssql_num_rows ( $Result );
//Print up on the screen how many rows where in their search
print "There were $Num_Rows of results for your search";
//Make the table
echo "<table border=2>\n";
while ( $a_row = mssql_fetch_row( $Result ) )
{
echo "</tr>\n";
foreach ( $a_row as $field ) { print "\t<td>$field</td>\n"; }
echo "</tr>\n";
}
"</table>\n";
//Everything is down close down and go home
mssql_close ( $link );
?>
</table>
</body>
</html>
----
Manual Page -- http://www.php.net/manual/en/ref.mssql.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+44407
Delete -- http://master.php.net/manage/user-notes.php?action=delete+44407&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+44407&report=yes
Search -- http://master.php.net/manage/user-notes.php