Connect to MSSQL using PHP Problem

Discussion in 'HTML / PHP / JavaScript / CSS' started by josepha, Nov 13, 2012.

  1. Hello,

    We got 3 websites with hosted by DiscountASP, and i was trying to connect to on of the databases using PHP code, and i was not able to do it. So i tired the same code on another server and its working, but i need it to work on all sites.

    the code is correct but the server is not letting me to connect, the web server is web155, and it's working on web154.

    the code is:

    $serverName = "sql2k803.discountasp.net";
    $uid = "****_user";
    $pwd = "****";
    $databaseName = "****";

    $connectionInfo = array( "UID"=>$uid,
    "PWD"=>$pwd,
    "Database"=>$databaseName);
    $conn = sqlsrv_connect( $serverName, $connectionInfo);
    if( !$conn )
    {
    die( print_r( sqlsrv_errors(), true));
    } else {
    die ("It connected");
    }

    Can one of the admins check it please?
    Thanks
     
  2. martino

    martino DiscountASP.NET Staff

    How is it not working exactly? Do you get any error messages?

    Are you connecting to the same SQL database?
     
  3. martino,

    It just not working, its even not returning an error with print_r( sqlsrv_errors(), true), I i replace this line with "echo not connected" it will return the setance and that means its not connecting.

    As I mentioned I am using this code now, but on another server hosted by you, but to a different Database (I am not connecting to a different Server DB).
     
  4. martino

    martino DiscountASP.NET Staff

    I would say double check your connection settings. Make sure you are using the correct user name, password, database name, and server name. (Some times these things are over looked.)

    Also, Try to connect to the database directly with SQL Server Management Studio. Using the same settings just to make sure you are able to make a connection to the SQL database.

    Also, try using our sample code in our knowledge base article here: Sample code to connect to an MS SQL database using php
     
  5. I did all that, I connected to the DB using SQL Server Management Studio with the same settings, And I tired that code you mentioned.

    I am 100% sure there is a problem with the server DB, since you are one of the staff, you can try it by your self (server: web155)

    Thanks :)
     
  6. martino

    martino DiscountASP.NET Staff

    I just tested our sample script on our test accounts one on WEB155. The other one in WEB154.

    The sample script worked correctly.

    Since you stated that you tested the sample script. Can you please provide us with the error message? I know that you originally stated that your script doesn't produce an error but our sample script should provide you with some kind of error message if there is a problem.

    Also in our sample code here:

    PHP:
    <?php  
    $serverName 
    "ServerName"
    $uid "sqlusername";   
    $pwd "sqlpassword";  
    $databaseName "DBName"
     
    $connectionInfo = array( "UID"=>$uid,                            
                             
    "PWD"=>$pwd,                            
                             
    "Database"=>$databaseName); 
      
    /* Connect using SQL Server Authentication. */  
    $conn sqlsrv_connect$serverName$connectionInfo);  
      
    $tsql "SELECT id, FirstName, LastName, Email FROM tblContact";  
      
    /* Execute the query. */  
      
    $stmt sqlsrv_query$conn$tsql);  
      
    if ( 
    $stmt )  
    {  
         echo 
    "Statement executed.<br>\n";  
    }   
    else   
    {  
         echo 
    "Error in statement execution.\n";  
         die( 
    print_rsqlsrv_errors(), true));  
    }  
      
    /* Iterate through the result set printing a row of data upon each iteration.*/  
      
    while( $row sqlsrv_fetch_array$stmtSQLSRV_FETCH_NUMERIC))  
    {  
         echo 
    "Col1: ".$row[0]."\n";  
         echo 
    "Col2: ".$row[1]."\n";  
         echo 
    "Col3: ".$row[2]."<br>\n";  
         echo 
    "-----------------<br>\n";  
    }  
      
    /* Free statement and connection resources. */  
    sqlsrv_free_stmt$stmt);  
    sqlsrv_close$conn);  
    ?> 
    Notice the line here:

    PHP:
    $tsql "SELECT id, FirstName, LastName, Email FROM tblContact"
    You need to change these settings to fit your database. For example: tblContact is a table in your database. If this table doesn't exist in the SQL database. Then it will not work.

    You also need to change the following column names to fit the column names in your table. id, FirstName, LastName, Email (Will need to changed to your own column names.)
     

Share This Page