Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Cannot Select DB using mssql

  1. #1
    Join Date
    Jul 2010
    Posts
    228
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default 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 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

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    try changing
    ... or die("Cannot select DB");
    to
    ... or die('MSSQL error: ' . mssql_get_last_message());
    and see what (if) error message is being returned.

  3. #3
    Join Date
    Jul 2010
    Posts
    228
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by traq View Post
    try changing
    ... or die("Cannot select DB");
    to
    ... or die('MSSQL error: ' . mssql_get_last_message());
    and see what (if) error message is being returned.
    I try the code you suggested:
    PHP Code:
    $db "dbtest"
    $selected mssql_select_db($db) or die('MSSQL error' mssql_get_last_message()); 
    and the output is:

    MSSQL error.The server principal "[domain nmae]\IUSR_[computer name]" is not able to access the database "dbtest" under the current securioty context.

    Honestly, I have no idea about this error.

  4. #4
    Join Date
    Jul 2010
    Posts
    228
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by traq View Post
    try changing
    ... or die("Cannot select DB");
    to
    ... or die('MSSQL error: ' . mssql_get_last_message());
    and see what (if) error message is being returned.
    I try this code:

    PHP 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.

  5. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    unfortunately, I don't know much about mssql. It sound like your database might not be configured to accept connections from your script...? Is the database on the same server as the website? Beyond that, I'm not sure what more I can offer you.

    Edit:

    I found some possibly-helpful comments on php.net: mssql_connect.



    Maybe someone else with more experience with mssql can help out?

  6. #6
    Join Date
    Jul 2010
    Posts
    228
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by traq View Post
    unfortunately, I don't know much about mssql. It sound like your database might not be configured to accept connections from your script...? Is the database on the same server as the website? Beyond that, I'm not sure what more I can offer you.

    Edit:

    I found some possibly-helpful comments on php.net: mssql_connect.



    Maybe someone else with more experience with mssql can help out?
    Yes, the database and website is on the same server.

  7. #7
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    If you log in to the database using the username and password you have in the php connection file can you run the select?
    Corrections to my coding/thoughts welcome.

  8. #8
    Join Date
    Jul 2010
    Posts
    228
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by bluewalrus View Post
    If you log in to the database using the username and password you have in the php connection file can you run the select?
    I'm using Windows Authentication mode when i connect to SQL Server, so I have no username and password need to input because the username and password was hide. But my authentication mode is mixed.

    On my database I have 5 users here are the list:

    dbo
    guest
    INFORMATION_SCHEMA
    DOMAIN\SQLServer2005MSSQLUser$COMPUTER NAME$SQLEXPRESS
    sys

    I don't know which user I am using when I connecting.

  9. #9
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Try querying

    SELECT SYSTEM_USER
    I've never used this and can't test this currently but it looks like it should work to tell you who you are querying as.
    Corrections to my coding/thoughts welcome.

  10. #10
    Join Date
    Jul 2010
    Posts
    228
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by bluewalrus View Post
    Try querying



    I've never used this and can't test this currently but it looks like it should work to tell you who you are querying as.
    Thank you,

    I found the list of users from my database name->Security->Users

    here are the users listed:

    dbo
    guest
    INFORMATION_SCHEMA
    DOMAIN\SQLServer2005MSSQLUser$COMPUTERNAME$SQLEXPRESS
    sys

    but when I run the SELECT SYSTEM_USER, the output is DOMAINNAME\Administrator,now which is the user in the listed,Is it the DOMAIN\SQLServer2005MSSQLUser$COMPUTERNAME$SQLEXPRESS?

    Thank you

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •