Connecting Tech Pros Worldwide Forums | Help | Site Map

Connecting PHP to MS-SQL Server

Member
 
Join Date: Feb 2007
Location: Outside of Milwaukee, WI
Posts: 116
#1: May 2 '07
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($get_item_sql);
ECHO($OUTPUT);
MSSQL_CLOSE();
?>
[/PHP]

I am running xampp on a winXP(pro) using sql server 2005

Newbie
 
Join Date: Apr 2007
Posts: 10
#2: May 2 '07

re: Connecting PHP to MS-SQL Server


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

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     MSSQL_connect("ERIC2005","sa","sa") or die ("can't connect to SQL server");
  3.     mssql_select_db("PS_SAMPLE_DB") or die ("can't select database");
  4.     $get_item_sql = "SELECT * FROM PS_ACAD_PLAN";
  5.     $OUTPUT =  MSSQL_QUERY($get_item_sql);
  6.       ECHO($OUTPUT);
  7.     MSSQL_CLOSE();
  8. ?> 
  9.  
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.

Expand|Select|Wrap|Line Numbers
  1.  $con = MSSQL_connect("ERIC2005","sa","sa") or die ("can't connect to SQL server");
  2.  
Expand|Select|Wrap|Line Numbers
  1. $OUTPUT =  MSSQL_QUERY($get_item_sql, $con);
  2.  
you also need to change how you are presenting the data:

Expand|Select|Wrap|Line Numbers
  1.  
  2. while ($row = mssql_fetch_array($OUTPUT))
  3. {
  4. echo $row['column_name'];
  5. }
  6.  
  7.  
so let me put this together

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.     $con = MSSQL_connect("ERIC2005","sa","sa") or die("can't connect");
  4.  
  5.      $db = mssql_select_db("PS_SAMPLE_DB") or die ("can't connect to db");
  6.  
  7.     $get_item_sql = "SELECT * FROM PS_ACAD_PLAN";
  8.  
  9.     $OUTPUT =  MSSQL_QUERY($get_item_sq, $con);
  10.  
  11.     while ($row = mssql_fetch_array($OUTPUT))
  12.     {
  13.            echo $row['column_name'];
  14.     }
  15.  
  16.     MSSQL_CLOSE();
  17. ?>
  18.  
see if that helps :)
Member
 
Join Date: Feb 2007
Location: Outside of Milwaukee, WI
Posts: 116
#3: May 2 '07

re: Connecting PHP to MS-SQL Server


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
Newbie
 
Join Date: Apr 2007
Posts: 10
#4: May 2 '07

re: Connecting PHP to MS-SQL Server


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
Expand|Select|Wrap|Line Numbers
  1. echo($row['column_name']);
  2.  
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 :)
Member
 
Join Date: Aug 2006
Posts: 110
#5: May 3 '07

re: Connecting PHP to MS-SQL Server


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($get_item_sq, $con);

if ($OUTPUT)
{

while ($row = mssql_fetch_array($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
Member
 
Join Date: Feb 2007
Location: Outside of Milwaukee, WI
Posts: 116
#6: May 3 '07

re: Connecting PHP to MS-SQL Server


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
Reply