Viewed   412 times

So I keep getting this error when I want to query something to the ms sql server..

The connection is made with the database but the queries seem to fail.

The error log contains this:

    PHP Fatal error:  Call to undefined function mssql_query()

The code on the php:

session_start();
include_once("connect.php");
if (isset($_POST['username'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM test WHERE username='".$username."' AND password='".$password."'";
$res = mssql_query ($sql) or die(mssql_error());

if (mssql_num_rows($res) == 1) {
    $row = mssql_fetch_assoc($res);
    $_SESSION['uid'] = $row['id'];
    $_SESSION['username'] = $row['Username'];
    $_SESSION['afdeling'] = $row['Afdeling'];
    $_SESSION['mail'] = $row['Mail'];
              header("Location: test.php");
    exit();
} else {
    echo "Invalid login information. Please return to the previous page.";
    exit(); }  }  ?>

Does anybody knows what the problem is?

Thanks in advance!

connect.php code:

<?php
$serverName = "MTN-TEST"; //serverNameinstanceName
$connectionInfo = array( "Database"=>"PROCES_TEST", "UID"=>"blaaa", "PWD"=>"blooo");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "<span style='color:green;'>Connection established.</span><br />";
}else{
     echo "<span style='color:red;'>Connection could not be established.</span><br />";
    die( print_r( sqlsrv_errors(), true));
}
?>

 Answers

1

You don't have the MS SQL Drivers installed. You can check this with phpinfo();

On Linux you need mssql.so or sybase.so With debian its apt-get install php5-sybase

For windows take a look here: http://msdn.microsoft.com/en-US/library/cc793139%28v=SQL.90%29.aspx

Drivers need to be configured for PHP to find the function mssql_...

You could also look at PDO DB classes as they can connect to any DBS, you need the drivers installed tho.

Sunday, October 2, 2022
 
juwaini
 
2

No. Fatal errors are fatal. Even if you were to write your own error handler or use the @ error suppression operator, E_FATAL errors will still cause the script to halt execution.

The only way to handle this is to use function_exists() (and possibly is_callable() for good measure) as in your example above.

It's always a better idea to code defensively around a potential (probable?) error than it is to just let the error happen and deal with it later anyway.

EDIT - php7 has changed this behavior, and undefined functions/methods are catchable exceptions.

Wednesday, November 2, 2022
 
mage_xy
 
3

It happens because you are declaring the function after trying to call it:

tally($_POST[$qvar]);// LINE THAT CAUSES ERROR

function tally ($question) // TALLY FUNCTION DECLARATION COMES LATER

I would put either before if(isset($_POST['submit'])) { or as the first thing inside.

   if (!function_exists('tally')) {
   function tally ($question) // TALLY FUNCTION
       {   
            global $nowval;
            $nowval[$question]++;
       }   
  }

$this->tally won't work because it's not an Object, you are justing decrating a function.

Sunday, November 6, 2022
4

The problem is definitely with the Microsoft ODBC drivers version 11, and are fixed in ODBC Driver 13 Preview for SQL Server.

I discovered this as my development machine running ubuntu 14.04 with the Driver 13 Preview installed works fine, but then when I deployed to our production server running RHEL 7 with the Driver 11, all kinds of havoc ensued as column names were truncated at 15 chars.

Microsoft's documentation is lackluster for Linux support, so if you're running ubuntu, then here's the gist of getting it installed.

Tuesday, September 27, 2022
 
4

I got it figured out. I had to install the ODBC Driver 17 for SQL Server (msodbcsql_17.2.0.1_x64.msi) on my server. The SQL Server Native Client 11.0 was installed but not the ODBC Driver for SQL Server.

For future reference for anyone else with this or a similar issue...

It can be downloaded at https://www.microsoft.com/en-us/download/details.aspx?id=56567 (note: if you have a 32 bit server, you will want to install the msodbcsql_17.2.0.1_x86.msi - If you accidentally try to install the incorrect version, it will let you know during the installation). After the driver is installed, you need to reboot the server. It won't prompt you to restart, but you'll need to.

In my PHP.ini I have added extension=php_pdo_sqlsrv_72_nts.dll and extension=php_sqlsrv_72_nts_x64.dll They can be downloaded at https://docs.microsoft.com/en-us/sql/connect/php/system-requirements-for-the-php-sql-driver?view=sql-server-2017

More info can be found at https://docs.microsoft.com/en-us/sql/connect/php/loading-the-php-sql-driver?view=sql-server-2017 and https://docs.microsoft.com/en-us/sql/connect/php/system-requirements-for-the-php-sql-driver?view=sql-server-2017

I can now establish a connection to Sql Server using either sqlsrv_connect or PDO.

PDO connection test:

$SqlServer = "THISSERVERSQLEXPRESS";
$SqlServerCon = new PDO("sqlsrv:server=$SqlServer;Database=TheDatabase", "DbUName", "DbPassword"); 
if (!$SqlServerCon) {die('Unable To Connect to Sql Server');}
else
{echo "Connection Successful";} 

sqlsrv_connect connection test:

$SqlServer = "THISSERVERSQLEXPRESS";
$DbConnInfo = array( "Database"=>"TheDatabase", "UID"=>"DbUName", "PWD"=>"DbPassword");
$SqlServerCon = sqlsrv_connect( $SqlServer, $DbConnInfo);
if( $SqlServerCon ) {echo "Connection established";}
else
{echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));}
Friday, September 16, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :