-1

I am having an issue with PHP. I can query from MSSMS directly to the DB but my web pages all will not connect to the Server..

SQL Server 2016

PHP version 5.2.4 Sample:

$DatabaseServer =   'server';
$DatabaseUser =     'reader';
$DatabasePassword = ''; 
$DatabaseName =     'FSDBMV';   

$SQL_db = @mssql_connect($DatabaseServer,$DatabaseUser,$DatabasePassword)     or die("Unable to connect to Database server");
mssql_select_db("$DatabaseName");

$Sql = " SELECT   [CustomerID]
    ,[CustomerName]
    ,[CSR]
    ,[CommissionCode]

    FROM [FSDBMV].[dbo].[FS_Customer] as C
Zhorov
  • 28,486
  • 6
  • 27
  • 52
kvngarner
  • 9
  • 3
  • See [`mssql_get_last_message()`](http://php.net/manual/function.mssql-get-last-message.php). You need to find out _why_ it's not working – Phil Nov 20 '18 at 01:20

1 Answers1

1

Explanations:

First, remove error control operator @ and check for errors with mssql_get_last_message(). Then, execute your statement with mssql_query().

Example:

<?php
# Settings
$DatabaseServer   = 'server';
$DatabaseUser     = 'reader';
$DatabasePassword = ''; 
$DatabaseName     = 'FSDBMV';   

# Connection
$SQL_db = mssql_connect($DatabaseServer, $DatabaseUser, $DatabasePassword);
if ($SQL_db === false) {
    echo "Error (mssql_connect): ".mssql_get_last_message();
    exit;
}
if (!mssql_select_db($DatabaseName, $SQL_db)) {
    echo "Error (mssql_select_db): ".mssql_get_last_message();
    exit;
};

# Query
$Sql = "
    SELECT
    [CustomerID]
    ,[CustomerName]
    ,[CSR]
    ,[CommissionCode]
    FROM [FSDBMV].[dbo].[FS_Customer] as C
";
$stmt = mssql_query($Sql, $SQL_db);
if (!$stmt) {
    echo "Error (mssql_query): ".mssql_get_last_message();
    exit;
}

# Results
while ($row = mssql_fetch_assoc($stmt)) {
    echo print_r($row, true)."</br>";
}

# End
mssql_free_result($stmt);
mssql_close($SQL_db);
?>

Notes:

MSSQL feature was removed in PHP 7.0. Consider another way to connect to MS SQL Server.

Zhorov
  • 28,486
  • 6
  • 27
  • 52
  • Thank you for the response. I got the same error as with the original code. "Fatal error: Uncaught Error: Call to undefined function mssql_connect() in C:\xampp\htdocs\test.php:10 Stack trace: #0 {main} thrown in C:\xampp\htdocs\test.php on line 10" – kvngarner Nov 20 '18 at 12:34
  • 1
    @kvngarner Your `mssql` extension is not installed correctly. – Zhorov Nov 20 '18 at 12:37