473,473 Members | 1,460 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Connecting PHP to MS-SQL Server

116 New Member
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
May 2 '07 #1
5 3145
JMathews
10 New Member
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 :)
May 2 '07 #2
phytorion
116 New Member
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
May 2 '07 #3
JMathews
10 New Member
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 :)
May 2 '07 #4
deepaks85
114 New Member
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
May 3 '07 #5
phytorion
116 New Member
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
May 3 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Sacha Korell | last post by:
First of all, sorry for cross-posting, but this newsgroup is much busier than "aspnet.mobile". I'm trying to create a portal page that redirects depending on the device that is connecting. ...
5
by: csgraham74 | last post by:
Hi, I have recently decided to start using MYSQL instead of MS Access 2003. After reading through numerous articles i have learnt that i can use access as a GUI for making changes etc to the...
4
by: Logan | last post by:
I am interested in programmatically connecting my computer to my cell phone's bluetooth Hands-Free Profile (HFP). So can can use my computer to answer and make calls as well as route the audio...
3
by: olafbrungot | last post by:
I get the following error message when trying to connect to a host database (OBCD connection) with "MS Query". SQL0471N Invocation of routine "SYSIBM .SQLTABLES " failed due to reason...
0
by: fatimang | last post by:
can someone give me codes or things to use on connecting Python to MS Access. thank you in advance.. GOD BLESS..
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.