Advanced Search

Results 1 to 5 of 5

Thread: connecting php to ms sql server 2005

  1. #1
    Join Date
    Mar 2007
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default connecting php to ms sql server 2005

    hi i h to connect php to ms sql server 2005 and i hv no clue how to do it.. can some1 pls help.. i'll be very greatful
    thank in advance..Suk

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,626
    Thanks
    6
    Thanked 107 Times in 107 Posts

  3. #3
    Join Date
    Mar 2007
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    well i found those links using google but could not really follow them. i'm pretty new to php n was using mysql till now.. can u help with anythin more?please??

  4. #4
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,626
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Have you got the following code in which they are demonstrating the connection and retrieval data using PHP from an MS SQL DB

    Code:
    <?php
    /*Connect to the local server using Windows authentication and specify
    the AdventureWorks database as the database in use. */
    $serverName = "(local)";
    $connectionInfo = array( "Database"=>"AdventureWorks");
    if( !($conn = sqlsrv_connect( $serverName, $connectionInfo)))
    {
         echo "Could not connect.\n";
         die( print_r( sqlsrv_errors(), true));
    }
    
    /* Set up the T-SQL query. */
    $tsql = "SELECT ReviewerName, 
                    ReviewDate,
                    Rating, 
                    Comments 
             FROM Production.ProductReview 
             WHERE ProductID = ? 
             ORDER BY ReviewDate DESC";
    
    /* Set the parameter value. */
    $productID = 709;
    $params = array( $productID);
    
    /* Execute the query. */
    if( !($stmt = sqlsrv_query($conn, $tsql, $params)))
    {
         echo "Error in statement execution.\n";
         die( print_r( sqlsrv_errors(), true));
    }
    
    /* Retrieve and display the data. The first and third fields are
    retrieved according to their default types, strings. The second field
    is retrieved as a string with 8-bit character encoding. The fourth
    field is retrieved as a stream with 8-bit character encoding.*/
    while ( sqlsrv_fetch( $stmt))
    {
       echo "Name: ".sqlsrv_get_field( $stmt, 1 )."\n";
       echo "Date: ".sqlsrv_get_field( $stmt, 2, 
                           SQLSRV_PHPTYPE_STRING( SQLSRV_ENC_CHAR))."\n";
       echo "Rating: ".sqlsrv_get_field( $stmt, 3 )."\n";
       echo "Comments: ";
       $comments = sqlsrv_get_field( $stmt, 4, 
                                SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_CHAR));
       fpassthru( $comments);
       echo "\n"; 
    }
    
    /* Free statement and connection resources. */
    sqlsrv_free_stmt( $stmt);
    sqlsrv_close( $conn);
    ?>

  5. #5
    Join Date
    Mar 2007
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Error

    its giving me the following error:

    Fatal error: Call to undefined function sqlsrv_connect()

    i think this code is for sql server 2000. i need to connect to 2005

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
  •