Cannot Select DB using mssql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newphpcoder

    #1

    Cannot Select DB using mssql

    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:
    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
    Last edited by Markus; Oct 12 '10, 01:18 PM. Reason: Fixed code tags
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Use mssql_get_last_ message to get an error string.

    Comment

    • Reza Ruslan

      #3
      i think you should add "$conn" (without quotes) as your db resource link identifier..

      CMIIW ... ^_^

      Comment

      Working...