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