PDA

View Full Version : cannot connect to database


marko
14-05-2011, 03:30 PM
please help me I'm desperate!!

I have installed PHP version 5.2 - fine
I have SQL Server 2008 - fine

Operating system is Windows Server 2008

I ran PHP Info and it shows SQL Server loaded as an extension - fine

correct DLL's installed including the one that needs to be placed in the windows folder.

I am connecting from a standard database connection script (see below) and it keeps coming back as Couldn't connect to server

it pauses for a few moments as if it were trying to connect and then spits back the message. If I keep trying it will actually lock the user account in SQL Server and I will have to log in to SQL Server unlock it.

This seems as if it is trying to connect but the password is wrong but this cant be..... AND if I log in with the same user name and password into SQL Server that I use in my database connection script, it logs in fine.

What have I missed? it doesn't make sense why it wont connect!!!

The connection script:

$dbhost = 'localhost';
$dbusername = 'theusername';
$dbpasswd = 'thepassword';
$database_name = 'mydatabasename';

$connection = mssql_pconnect("$dbhost","$dbusername","$dbpasswd")
or die ("Couldn't connect to server.");

$db = mssql_select_db("$database_name", $connection)
or die("Couldn't select database.");

Botman
14-05-2011, 06:29 PM
Could it be that your user doesn't have permission to connect from localhost?

scross
15-05-2011, 09:26 AM
is the script on the same server as the db?

nebbian
15-05-2011, 09:44 AM
If you do this:

error_reporting(E_ALL);

and then check your web server's error log, does it give you a bit more information?

Also can you find your database server log? That may be the best place to start looking.

In MySQL (not sure about SQL Server) you can set up different permissions for user@localhost compared to user@anywhereelse. This may be what's happening here.

Webolution
15-05-2011, 07:29 PM
If you have access to SQL Profiler, run a new trace on the SQL Server instance you're connecting to. Then run your script and see if the trace has any information - at least you'll know if SQL is even getting the request or not.

Basil
16-05-2011, 10:46 AM
Try outputting mssql_get_last_message() either in the die() or remove the die() and just echo it. This should output the last error message from the sql server.

Basil
16-05-2011, 05:40 PM
It shouldn't make any difference... but I would also remove the quotes around your variables.

$connection = mssql_pconnect($dbhost,$dbusername,$dbpasswd)
or die ("Couldn't connect to server: " . mssql_get_last_message());

mssql_select_db($database_name, $connection)
or die("Couldn't connect to database: " . mssql_get_last_message());