0

We have a database running on Windows Server 2008 R2, SQL Server 2008 R2. I need to query the database via PHP (I'm also open to other options) and display the data in a web page.

I have tested connectivity with my username, password and server name using a UDL test file. My connection is successful.

My web server has PHP 5.4 installed and working.

I have built a PHP script that is supposed to connect and query the database but I get a server error.

<?php
$myServer = "ipaddress";
$myUser = "username";
$myPass = "password";
$myDB = "dbname"; 

//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
  or die("Couldn't connect to SQL Server on $myServer"); 

//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
  or die("Couldn't open database $myDB"); 

//declare the SQL statement that will query the database
$query = "SELECT fullName, email_address, pos_desc";
$query .= " FROM dbo.EMPLOYEE_VIEW ";
$query .= "WHERE grp_cde='STAFF'"; 

//execute the SQL query and return records
$result = mssql_query($query);

$numRows = mssql_num_rows($result); 
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>"; 

//display the results 
while($row = mssql_fetch_array($result))
{
  echo "<li>" . $row["id"] . $row["name"] . $row["year"] . "</li>";
}
//close the connection
mssql_close($dbhandle);
?>

Keep in mind the Database server and the Web Server are both running Windows Server 2008 R2 and exist in the same domain.

The only error I receive is: 500 - Internal server error. There is a problem with the resource you are looking for, and it cannot be displayed.

Is there anything I can do to troubleshoot why this is not working?

I appreciate any and all help on this question.

6
  • share your error, that will be easier to reply. Commented Jun 6, 2013 at 13:27
  • The only error I receive is: 500 - Internal server error. There is a problem with the resource you are looking for, and it cannot be displayed. Commented Jun 6, 2013 at 13:33
  • Did you change the Server parameter in first 4 line with correct values ? ipaddress should be either localhost or 127.0.0.1, similarly username and password should be one used in your database server. Just asking if you did change them. Commented Jun 6, 2013 at 13:34
  • Yes, i tested my server name, username and password with a utility that tests for a successful connection. The results came back successful. Although since the msSQL server is running on a different server I am using the ip address of that server and not localhost. Commented Jun 6, 2013 at 13:36
  • I think it might have something to do with the PHP configuration or the msSQL server configuration. Oh by the way our old web server is currently using asp.net to query it and it works. Commented Jun 6, 2013 at 13:38

2 Answers 2

1

Error 500 usually means you forgot a ; or ', I suggest you check your code carefully for any redundant or missing characters.

Also, turning on php errors in your php.ini file helps alot with fixing these sort of stuff.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your input Wesley, I am going to look up how to turn on php errors and see if I can pull any additional info for this problem.
@Chaddly You could put <?php ini_set('display_errors', true) ?> at the top of your form, maybe you can see another error then.
0

After pheww... what now? 6 hours? I have it working! The problem was that I needed to set up SQL Server 2008 drivers on my web server and connect using ODBC via my PHP script. I am now pulling and displaying data from my SQL Server 2008 Database like a pro. Thanks for the assistance guys.

odbc_connect("DRIVER={SQL Server Native Client 10.0};Server=serverip;Database=tablename",         "username", "pass");

1 Comment

i am using a shared windows server 2008 on the cloud. I also have this issue, how can i know if sql server native client 10.0 is installed?Also my connection string is sqlsvr_connect(). Will this also requires native client? Pls I need assitance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.