0

I am trying to export data from MSSQL table to Excel. Below is my code. Problem is that i am getting the following error:

    Fatal error: Call to undefined function mssql_fetch_array() in C:xampp\code.php on line 25

I am running Windows Server 2008 R2, IIS7, SQL Server 2008, PHP Version 5.4.4.

I have un-commented the line: "extension=php_mssql.dll" found in C:\xampp\php\php.ini

    <?php
// load library
require 'include\php-excel.class.php';

$i = 0; // used as a counter

$myServer = "SERVERNAME\SQLEXPRESS";
$myUser = "UserName";
$myPass = "xxxxxx";
$myDB = "dbName";

$conn = new COM ("ADODB.Connection") or die("Cannot start ADO");

$connStr = "PROVIDER=SQLOLEDB;SERVER=".$myServer.";UID=".$myUser.";PWD=".$myPass.";DATABASE=".$myDB;
$conn->open($connStr); //Open the connection to the database

$result = "SELECT Col1, Col2, Col3 FROM myTable WHERE Col3='myCondition'";

//$rs = $conn->execute($result);
//$num_columns = $rs->Fields->Count();

    // create data array and print headers on the first row
    $data = array(1 => array ('No.', 'Col1', 'Col2', 'Col3'));

    while($row=mssql_fetch_array($result)) {
//include additional rows
array_push($data, array($i, $row['Col1'], $row['Col2'], $row['Col3']));
$i++;
}

// If no results, indicate this on the first row
if ($i == 0){
$data = array(1 => array ('No results', 'empty', 'empty', 'empty'));
                        }

//generate file (constructor parameters are optional)
$xls = new Excel_XML('UTF-8', false, 'My Test Sheet');
$xls->addArray($data);
$xls->generateXML('MyReport');
    ?>

Thank you in advance.

4
  • Did you verify that it was the correct ini file, and restart IIS after enabling the module? (Check for it in phpinfo()'s output Commented Aug 14, 2012 at 14:15
  • @ctrahey is correct. Run phpinfo() and see if the MS SQL functions are enabled. Commented Aug 14, 2012 at 14:32
  • Loaded Configuration File: C:\xampp\php\php.ini and that is where i edited/un-commented the "extension=php_mssql.dll" file then restarted webserver. Commented Aug 14, 2012 at 14:38
  • I restarted both IIS7 and Apache but same error message. Commented Aug 14, 2012 at 15:11

4 Answers 4

1

PHP5.3 onwards doesn't support the mssql functions. If you check your PHP ext folder, you will find there isn't a php_mssql.dll no more

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

Comments

1

You're using an ADODB COM to create your DB connection. This isn't the same thing as using the mssql_xx library.

You can't mix and match between the two - they're not compatible.

In any case, you clearly don't have the mssql_xx library installed (I'm not even sure it's supported any more), so you can't use that.

Right above your failing call to mssql_fetch_array(), you have some commented out lines:

//$rs = $conn->execute($result);
//$num_columns = $rs->Fields->Count();

Not sure why you've commented these out, because you'll need them, or something like them. Particularly the first of those lines. And after that, you'd need to use the ADODB equivalent to mssql_fetch_array() to read the records in the loop.

However, my advice is to ditch all of this, and switch to using the PDO library instead. PDO supports the MS SQL database, and is standard PHP, unlike the ADODB stuff you're trying to use here.

See the PHP manual for the PDO library here: http://php.net/manual/en/book.pdo.php

1 Comment

Thanks @SDC very much, very important note for everyone that you have to Pay attention on what Library you use to create your DB connection in my case Ubuntu 16.04 PHP7 MSSQL server and I use FreeTDS which use PDO JUST as SDC mentioned here Hence I need to use the right support function for it.
0

After uncommenting the line for MS SQL library, have you restarted the web server for the change to take effect?

1 Comment

These should be comments on the question (and the info bout a restart not fixing it should be an edit to the question)
0

For sql, we can use sqlsrv_fetch_array with sqlsrv_query as:

$sql_query = "SELECT Col1, Col2, Col3 FROM myTable WHERE Col3='myCondition'";
$result = sqlsrv_query($conn,$sql_query);
while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_ASSOC) ) {
        echo $row['Col1'];
        /*etc codes here*/
}

Comments

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.