Trouble connecting to SQL Server with PHP - php

I'm trying to connect to my local SQL Server 2008 R2 (have also tried it with 2005, same result exactly) with PHP. I'm using PHP 5.1 which still supports php_mssql and mssql_connect().
for some reason PHP just won't find my server, I can connect via ODBC flawlessly an that's fine, but I would like to connect to SQL Server directly.
I have connected PHP to SQL Server a million times on different servers, this one seems to be the only one giving me issue.
This is my little test code to try and get the connection working.
//define connection garbage
$db['hostname'] = "USER90C6\SQLEXPRESS";
$db['username'] = "user";
$db['password'] = "password";
$db['database'] = "kal_auth";
//connection string
$conn = mssql_connect($db['hostname'], $db['username'], $db['password']);
//does it work? :o
if($conn)
{
echo "works";
}
else
{
echo "fails";
}
The error this code produces:
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: USER90C6\SQLEXPRESS in C:\xampp\htdocs\test.php on line 9
fails
Is there something seriously wrong with my setup? or am I missing something.

Did you enable TCP/IP on the server's configuration tool?
Is the firewall blocking any ports you might be using?
Are your MDAC (microsoft data access components) updated?
turn on mssql.secure_connection in php.ini

I think you don't miss anything.. Your connection string seems to be right (you receive a "Unable to connect" error..).
In my opinion, you problem can be a version incompatibility or a user privileges mistake. First of all: look at DLL driver you are using in PHP and check it's compatibility with you MSSQL version.
Maybe can be a good idea a fresh PHP install, with the latest stable release, if it is possible. Give a look at: http://www.php.net/manual/en/mssql.requirements.php
Good luck.

Related

DB2 connection failed php

I'm trying to connect to remote DB2 via PHP. But have some problems. I've already installed IBM Application developer client.
phpinfo() output:
IBM DB2, Cloudscape and Apache Derby support enabled
Module release 1.9.4
Module revision $Revision: 327944 $
Binary data mode (ibm_db2.binmode) DB2_BINARY
Then, I've got a php file which is looking like:
$database = 'MyDB';
$user = 'db2inst1';
$password = 'mypassword';
$hostname = '1.1.1.1';
$port = 50000;
$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$database;PORT=$port;HOSTNAME=$hostname;".
"PROTOCOL=TCPIP;UID=$user;PWD=$password;";
$conn = db2_connect($conn_string, '', '');
if ($conn) {
echo "connection to $database succeeded";
} else {
echo "connection to $database failed";
echo db2_conn_errormsg();
}
And trying to execute this file, I have "connection to MyDB failed", and NO visible response from db2_conn_errormsg(), which is actually making me baffled
Unfortunately, I haven't got a straight access to the remote server with database. But several months ago, when I was using other client, I succeeded to connect to exactly this database. But that time I didn't need to install IBM ADCL. That is why I can guess that problem is on this side. But even if so, I couldn't fix it.
Sorry if I duplicated some question on stackoverflow, but all answers, which I found, were unfortunately useless to me.
I'm using Apache 2.2 and PHP 5.4.
Hope you can help.
Thanks for any replies!
Are you sure you have connectivity to the server? Correct port, server, firewall rules, username, password, database name?
What is the SQL code you are receiving. Try to get the SQL code from PHP, "connection to xx failed" is your own code so it is useless to help you.
Did you install the application development client? which DB2 version are you using? ADCL is old, it was for DB2 8. Since DB2 9.7, clients have different names, and I think you need IBM Data server client in order to compile the php module. For more information, check this website: http://www-01.ibm.com/support/docview.wss?uid=swg27016878
I think you have to catalog the database server (node) and the database in the local machine with the db2 client. It seems that your PHP code uses ODBC driver, and it has to be configured locally.
Your connection string looks like an ODBC connection where as the db2_connect function in PHP needs :-
DATABASE=database;HOSTNAME=hostname;PORT=port;PROTOCOL=TCPIP;UID=username;PWD=password;
It's on the PHP web page.
I have never been able to get ibm_db2 or pdo_ibm working on a remote. They work if DB2 and Apache are on the same machine (like the iSeries) but not if the host is connecting to a remote DB2.
If you read the Doctrine2 PHP drivers for each you will find they redirect to ODBC if the host is not 'localhost' or '127.0.0.1'.
This code works for me
<?php
if (!$db = odbc_connect ( "AS400", $user, $password) )
echo 'error!';
$result = odbc_exec($db, "select count(*) from $table");
while (odbc_fetch_row($result)) {
var_dump($result);
print_r($result);
echo "\n";
echo odbc_result($result, 1)."\n";
}
odbc_close($db);
AS400 is DSN name defined in ODBC.

MySQL failing to establish a connection using PHP

Using ubuntu 12.04 64 bit on Lenovo t410.
Using apache2 and Mysql 5.5 and attempting to connect via localhost
I am attempting to establish a connection to a database that I made on localhost. When the line of code is reached to establish a connection, it seems Mysql simply hangs, and there is no error message displayed after. I verified that an echo works immediately prior to the connection attempt. I know that apache2 server is working as I can access the index page and display my html form.
I have tried etc/mysql/my.cnf setting the bind address to localhost.
My line of code looks like:
// Attempts to establish connection to MySql server
$connection = mysql_connect("localhost","username","password");
// Prints error message if the connection to MySql fails
if (!$connection){
die("Connection failed: " . mysql_error());
}
echo "Connection established.";
I tried the connection line with single quotes and with no semi-colon as well.
I am willing to post the contents of any configuration file I have if the error isn't syntax. I haven't done anything fancy to Ubuntu, everything is the default install. I am new to CS and especially databases, PHP, and networking. This is my little experiment that I am stuck on.
Any help is greatly appreciated. Thanks,
Don
Can it be, because there is no error message, that the connection IS established, but you didn't do anything with it?
I mean, what is the rest of your code, is there after your code here something like:
mysql_select_db("database_name",$connection);
After reading your last comment, it appears the mysql extensions are not being loaded. Have a look at your php.ini, uncomment the following line (remove the semicolon at the beginning of the line) and restart your apache:
extension=php_mysql.so
Make sure the extension exists in the php extensions directory.
Due to the fact that you are using MySQL version > 4.1.3 it is strongly recommended that you use the mysqli extension instead. Have a look at this: PHP: MySQL Overview
try to set
$mysql_user = "your_username";
$mysql_pass = "your_password";
$mysql_server = "Servername";
$link = mysql_connect($mysql_server,$mysql_user,$mysql_pass);
if (!$link) {
header('HTTP/1.1 500');
exit();

FileMaker Pro PHP ODBC Connection

How do I connect to a FMP database using an ODBC connection?
The name of the server is fmserver, and there is no Username or Password.
This is what I try:
$name = 'fmserver';
$conn = odbc_connect($name, '', '');
But for some reason, when I try it out, it brings me to a page that says (on chrome):
Server error
The website encountered an error while retrieving [URL]. It may be
down for maintenance or configured incorrectly.
Is there some method that I'm implementing wrong? I have the IP address too, and have tried that, but I can't seem to get that to work either.
Do you have the FileMaker ODBC driver installed and configured with "fmserver" as the DSN?
I would highly recommend you take a look at the FileMaker PHP API vs. ODBC.
http://www.filemaker.com/support/technologies/php.html

Why won't my server connect to a remote MSSQL server using PHP mssql_connect?

I've had to move an app we wrote for a client to a new server and a remote connection I was initiating with PHP mssql_connect has ceased to work. I noticed that PHP wasn't compiled with mssql so I asked the server admin to install it. I can verify that it's now installed via PHP info but I now get a consistent "Unable to connect to server" error from mssql_connect.
Here's the very simple PHP script I'm running:
$myServer = "myserver.com:5000";
$myUser = "myusername";
$myPass = "mypassword";
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer. Error: " . mssql_get_last_message());
I've confirmed that the credentials are still correct but for whatever reason it seems that mssql_connect just isn't doing it's thing. I'm wondering if there's something that the admin has forgotton to do having installed the extension and FreeTDS. Any pointers greatly appreciated! :)
PROBLEM SOLVED!!!
After all that it turned out to be the FreeTDS protocol version number as specified in /usr/local/freetds/etc/freetds.conf, line number 13 had to be uncommented. That was it! :)
After all that it turned out to be the FreeTDS protocol version number as specified in /usr/local/freetds/etc/freetds.conf, line number 13 had to be uncommented. That was it! :)
There's a good chance that remote connections are disabled on the server where the server is installed. Have you checked whether you are actually able to connect to the DB server remotely, from the IP address of the webserver that attempts to connect to it? Usually, at least in most shared hosting schemes, the database server only accepts connections from localhost. You'd need to add the IP address of the webserver to a whitelist (usually in the server's configuration) to allow connections from the outside.

Connect to SQL Server 2008 with PDO

I am trying to connect to SQL Server 2008 (not express) with PHP 5.2.9-2 on Windows XP sp2. I can connect to the remote SQL Server fine from SQL Server Management Studio from the same machine.
My first attempt was this:
$conn = new PDO("mssql:host={$host};dbname={$db}", $user, $pass);
Which gives this error:
PHP Fatal error:
Uncaught exception 'PDOException' with message
'SQLSTATE[0100] Unable to connect: SQL Server is unavailable or does not exist.
Access denied. (severity 9)'
Second attempt (found on experts-exchange)
$conn = new PDO("odbc:Driver={SQL Server Native Client 10.0};Server={$host};Database={$db};Uid={$user};Pwd={$pass}",
$user, $pass);
This works, but I can't use PDO::lastInsertId(), which I would like to be able to:
Driver does not support this function: driver does not support lastInsertId()
Do you have any suggestions? Any help would be greatly appreciated.
i'm pretty sure that pdo with the mssql data server type requires dblib to connect. do you have dblib installed on the machine? make sure you can find ntwdblib.dll somewhere in the path on your system. i know this doesn't jive with the error message you're getting, but i'm not sure i trust the error message.
I updated ntwdblib.dll as suggested here and it now works.
Unfortunately I still can't use PDO::lastInsertId() because apparently this driver does not support it either, so it wasn't really worth the hassle. I can however use the equivalent SELECT ##IDENTITY as Id.
That you cannot use SQL Server authentication because only Windows authentication is permitted.
Check if the server is running Mixed mode authentication.
Also check if this SO question helps you.

Categories