Connecting PHP to MS-SQL Server

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phytorion
    New Member
    • Feb 2007
    • 116

    Connecting PHP to MS-SQL Server

    I just started trying to programm in PHP a couple of days ago and i can't seem to find any help on connecting and returning data from MS-SQL Server. I think i have all the connections working but when i run my code it just returns "Reference ID #3". with if i understand means no data is being returned but i don't understand where i'm going wrong. Here is my code:

    [PHP]
    <?php
    MSSQL_connect(" ERIC2005","sa", "sa");
    mssql_select_db ("PS_SAMPLE_DB" );
    $get_item_sql = "SELECT * FROM PS_ACAD_PLAN";
    $OUTPUT = MSSQL_QUERY($ge t_item_sql);
    ECHO($OUTPUT);
    MSSQL_CLOSE();
    ?>
    [/PHP]

    I am running xampp on a winXP(pro) using sql server 2005
  • JMathews
    New Member
    • Apr 2007
    • 10

    #2
    I would start by adding some debug strings into your php code:

    Code:
    <?php
        MSSQL_connect("ERIC2005","sa","sa") or die ("can't connect to SQL server");
        mssql_select_db("PS_SAMPLE_DB") or die ("can't select database");
        $get_item_sql = "SELECT * FROM PS_ACAD_PLAN";
        $OUTPUT =  MSSQL_QUERY($get_item_sql);
          ECHO($OUTPUT);
        MSSQL_CLOSE();
    ?>
    I would also create a variable for the connection to hand to your query. I know that most don't need to, but I don't think it hurts either.

    Code:
     $con = MSSQL_connect("ERIC2005","sa","sa") or die ("can't connect to SQL server");
    Code:
    $OUTPUT =  MSSQL_QUERY($get_item_sql, $con);
    you also need to change how you are presenting the data:

    Code:
    while ($row = mssql_fetch_array($OUTPUT))
    {
    echo $row['column_name'];
    }
    so let me put this together

    Code:
    <?php
        
        $con = MSSQL_connect("ERIC2005","sa","sa") or die("can't connect");
       
         $db = mssql_select_db("PS_SAMPLE_DB") or die ("can't connect to db");
       
        $get_item_sql = "SELECT * FROM PS_ACAD_PLAN";
        
        $OUTPUT =  MSSQL_QUERY($get_item_sq, $con);
     
        while ($row = mssql_fetch_array($OUTPUT))
        {
               echo $row['[I]column_name[/I]'];
        }
    
        MSSQL_CLOSE();
    ?>
    see if that helps :)

    Comment

    • phytorion
      New Member
      • Feb 2007
      • 116

      #3
      Thanks for the reply. I tried adding in the code and it did make the Resource id#3 go away but the screen is just blank now. any ideas?

      Eric

      Comment

      • JMathews
        New Member
        • Apr 2007
        • 10

        #4
        Generally, a blank screen indicates there is a syntax error in your script. So check to make sure that you are ok in that area first.

        I would also change the echo function output a bit since you seem to only want to print a variable
        Code:
        echo($row['column_name']);
        The return from a query in sql is an array, so you want to be sure that you are using the column names from the actual table :)

        Comment

        • deepaks85
          New Member
          • Aug 2006
          • 114

          #5
          Hi Dear,

          I saw your query and I would suggest to use in following way:

          [PHP]<?php

          $con = MSSQL_connect(" ERIC2005","sa", "sa") or die("can't connect");

          $db = mssql_select_db ("PS_SAMPLE_DB" ) or die ("can't connect to db");

          $get_item_sql = "SELECT * FROM PS_ACAD_PLAN";

          $OUTPUT = MSSQL_QUERY($ge t_item_sq, $con);

          if ($OUTPUT)
          {

          while ($row = mssql_fetch_arr ay($OUTPUT))
          {
          echo $row['column_name'];
          echo "<br>";
          }
          }

          MSSQL_CLOSE();
          ?>[/PHP]

          Try this and let me know if you get any bugs...........

          Good luck.

          Thanks
          Deepak

          Comment

          • phytorion
            New Member
            • Feb 2007
            • 116

            #6
            Thank you for your help it finally outputed the data. Now i just need to read up on getting the formatting down. Thanks again for the help.

            Eric

            Comment

            Working...