2

I need to get some data from a Microsoft SQL Server database at work. When I have the data I need, I need to make an Excel spreadsheet that can be saved locally on my computer.

I found PHPExcel which seems to do the job on the Excel part, but what about getting the data from the Database?

I can't seem to find anything that's recent. Only old tutorials.

4
  • Is it not possible for you to use Excel to connect directly to SQL server (data tab; From other sources -> From SQL Server) Commented Jun 27, 2013 at 11:36
  • I didn't say I was gonna connect directly to the SQL with Excel. Commented Jun 27, 2013 at 11:37
  • I would query the database and then create a CSV file Commented Jun 27, 2013 at 11:38
  • It needs to be Excel. There is no alternative to this. Commented Jun 27, 2013 at 11:39

2 Answers 2

5

Use this way to Fetch the Records :

<?php
$hostname = "192.168.3.50";
$username = "sa";
$password = "123456";
$dbName = "yourdb";

MSSQL_CONNECT($hostname,$username,$password) or DIE("DATABASE FAILED TO RESPOND.");
mssql_select_db($dbName) or DIE("Database unavailable");

$query = "SELECT * FROM dbo.table";

$result = mssql_query( $query );

for ($i = 0; $i < mssql_num_rows( $result ); ++$i)
     {
         $line = mssql_fetch_row($result);
         print( "$line[0] - $line[1]\n");
     }
?>

This will fetch each rows from the Data Retrieve and Print on the Page. Use your Required format into that. I mean, Use html Table to show the data in well format.

Sign up to request clarification or add additional context in comments.

1 Comment

I am coming from a Java/C# background is it a 2D array with [row][column] ?
2

Use this code to get an data from Database.

<?php
// Server in the this format: <computer>\<instance name> or 
// <server>,<port> when using a non default port number
$server = '192.168.3.50';

// Connect to MSSQL
$link = mssql_connect($server, 'sa', 'sa');
if (!$link) {
    die('Something went wrong while connecting to MSSQL');
}
else{
    echo "connected ";


  mssql_select_db('Matrix') or die("Wrong DATAbase");

   //mssql_query("SELECT Seq_no from dbo.Trans_R WHERE Seq_no = 000001",$link) or         die("cannot execute the query");

  $query = mssql_query("SELECT Tr_Date,Tr_Time,Tr_Data from Matrix.dbo.Trans_R");

$f = mssql_fetch_array($query);

    echo $f['Tr_Date'];

}
?>

Can i know why Negative Vote??

He asked me to :

" but what about getting the data from the Database?"

7 Comments

So, how would I shift through the data I get so I only extract the right data?
Now you can Add Criteria. Use Where Clause to Filter the Right data you want. To use Where Clause Checkout this : msdn.microsoft.com/en-us/library/ms188047.aspx
And, Can you please Specify your tables and Your requirements? So, that, we could help you easily?
The SQL Statement is not important. I will figure that out. It's really just how to fetch the data, and how to navigate the array or collection I get from executing the query.
So, What this mean : So, how would I shift through the data I get so I only extract the right data?. Didnt you mean to filter record you want?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.