5

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"; //serverName\instanceName
$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));
}
?>
3
  • 1
    php.net/manual/en/intro.mssql.php Commented Jun 14, 2013 at 8:02
  • @Innominatum : You have tagged this both mysql and sql-server. Which DB are you actually using ? Commented Jun 14, 2013 at 8:03
  • Are you sure connection to MS-SQL server is made correctly ? can you share code of connect.php ? Commented Jun 14, 2013 at 8:04

3 Answers 3

10

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.

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

2 Comments

Took a look into it, the mssql library is missing in PHPinfo(); Any idea where I can get it for windows? :)
That windows link to msdn did hit the spot. it works now Thank you!
3

If your connect.php code returns "Connection established.", it's mean you installed MS SQL Drivers correctly. You have to use sqlsrv_queryfunction instead of mssql_query. The correct form of this command is:

     <?php
                $serverName = "serverName"; 
                $options = array(  "UID" => "sa",  "PWD" => "Password",  "Database" => "DBname");
                $conn = sqlsrv_connect($serverName, $options);

                if( $conn ) {
                     echo "Connection established.<br />";

                     $query='select * from test';
                     $result = sqlsrv_query($conn,$query);

                }else{
                     echo "Connection could not be established.<br />";
                     die( print_r( sqlsrv_errors(), true));
                }

            ?>

you can learn more here:

"PHP Fatal error: Call to undefined function mssql_select_db() in c:\...appscript.php on line 16"

Comments

0

you can use mssql_get_last_message() for mssql errors

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.