Jump to content
Sign in to follow this  
kat35601

MS SQL PHP Connection Test Apache2 Server

Recommended Posts

I created this to test the connection to my ms sql server with a web page. I can connect with isql with out fail.

when I open this I get

 

PHP SQL Test

END PHP SQL Test

 

I was expecting to get sql data between the above lines. What am I doing wrong.

 

 

<html>
<head>
<title>PHP SQL Test</title>
</head>
<body>
<p> PHP SQL Test </p>

<?
$conn =odbc_connect("datasource","user","password");
if(!$conn) {
    exit("Connection Failed: " . $conn);
}

$sql="SELECT top 10 * from WIP_master";
$rs =odbc_exec($conn,$sql);
print_r($rs);
if(!$rs){
exit("Error in SQL");
}
odbc_close($conn);
?>
<p> End PHP SQL Test </p>
</body>
</html>

Share this post


Link to post
Share on other sites

Use the normal, long open tags. Short ones only work if you happen to have PHP set up to accept them. (= is an exception which will always work - with recent versions of PHP.)

Share this post


Link to post
Share on other sites

when I <?php I only get PHP SQL Test and no SQL output or END PHP SQL Test bythe way this is my first ever PHP code and I had to google long open tags to see what you meant. Thanks

Share this post


Link to post
Share on other sites

There may be other problems that aren't obvious...

 

You need to make a change to your php.ini file: run phpinfo() in a script and it will tell you where you can find the file near the top. Hopefully you do have one.

In there are two settings to change:

error_reporting = -1
display_errors = on
They exist in the file somewhere and probably in two locations: one place describes a bunch of settings, the other is the actual location where the setting is defined. Possibly commented out with a semicolon, in which case you need to uncomment it after changing the value.

 

When you do that, restart Apache and run phpinfo() again. Then look for those two settings - they should have the new values.

 

Then try your script again. There will probably be some kind of error message. Or more than one.

Share this post


Link to post
Share on other sites

Could you tell us how you are testing the code? You should be running the code from http://localhost and the code should be saved in a file ending in a .php extension.

Share this post


Link to post
Share on other sites

I installed php5-odbc and now I get Resource id#3. So it looks like its working I just now have to figure out how to get the results out. Thanks

 

 

PHP SQL Test

Resource id #3

End PHP SQL Test

Share this post


Link to post
Share on other sites

You need to use something like this to get the results from the query

// use a while loop to loop over the rows returned by the query
while($row = odbc_fetch_array($rs))
{
   echo '<pre>' . print_r($row, true) . '</pre>';; // show contents of row
}

Manual page on odbc_fetch_array

Edited by Ch0cu3r

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.