Cannot Select DB using mssql

Good day!

I change my database from mysql to mssql, so I need to change the connection code in my php. I’m not familiar about the codes using mssql. Now cannot connect to my database.
I used the following:
php 5.1.4
IIS 6.0
Microsoft SQL Server 2005 Express Edition
SQL Server Management Studio Express
Here is my code:


<?php
//session_start(); 
//session_regenerate_id(); 

//if($_SESSION['loggedin']){ 
//the user is already logged in, lets redirect them to the other page 
  //  header("Location:company.php"); 
//} 


$server = "ISM\\SQLEXPRESS";
//$db = "dbtest";
$connectOptions = array("Database" => "dbtest");
//Because UID and PWD are not specified in the connection option, the connection is made with Windows Authentication.
$conn = mssql_connect($server) or die("Could not connect");


$selected = mssql_select_db($connectOptions)or die("Cannot select DB");    


        $department = $_POST['department'];    
        $username = $_POST['username']; 

        $sql=mssql_query("SELECT `Department`, `Username` FROM `tbluser` WHERE `Department` = '{$department}' AND Username = '{$username}'") or die(mssql_min_error_severity()); 
        $ct = mssql_num_rows($sql); 
      
        if($ct == 1) { 
// im guessing this means that the user is valid. 
$_SESSION['loggedin'] = true; // now that the user is valid we change the session value. 
            $row = mssql_fetch_assoc($sql);   
			
			//$_SESSION['username'] = $row['Username'] ;
			//$_SESSION['department'] = $row['Department'];
			
			$Departments=array('aa', 'bb', 'cc', 'dd', 'ee', 'ff', 'gg', 'hh', 'ii', 'jj');
			
			if (in_array($row['Department'], $Departments)){
					header ('Location:company.php');
			}else{
					echo "Incorrect Username or Department";
					header ('Location:index.php');
			}
		}
?>

And when I run this code the result is:

Cannot select DB

Thank you in advance

I try a new code:


$db = "dbtest"; 
$selected = mssql_select_db($db) or die('MSSQL error' . mssql_get_last_message()); 

and the output is:
MSSQL error.The several principal “[domain name]\IUSR_[computer name]” is not able to access the databse “database name” under the current security context.

Then I research about this error and I find a step to fix the error, but I can’t follow the steps because I can’t find what they say in the steps.

here is the step:
To fix this SQL Server Management Studio 2008 (maybe in 2005 too), please do the following:

1.Open SQL Server Management Studio 2008
2.Connect to the database server as normal
3.Press F7 to open the “Object Explorer Details” window or click “View” >> “Object Explorer Details”
4.In the object explorer details window expand by double click on “Databases”
5.Right click on the column header and deselect “Collation” from the menu that will pop up
6.From the object explorer, refresh the “Databases”

I don’t know what the appearance is if I am already in the Object Explorer Details view. And I can’t see the column header and also the Collation.

can’t help you with the php code, but i noticed you are using backticks in the actual sql

that works in mysql, but it’s gonna fail in mssql

See the manual page for [fphp]mssql_select_db/fphp.

You’re passing an array of ‘options’ to the function, it does not support this. Pass the name of the database you wish to connect to and a, optional, connection(link_identifier). :slight_smile:

Thank you