Why doesn't sqlsrv_connect work like before? - php

So I've re-installed my "webserver". It had some OS issues and I decided it was time ! I copied/backed-up the entire WAMP folder and restored it nicely after reinstall. All of that works, just not the site like it should. In essence, the sqlsrv_connect seems not to connect ! It doesn't give me any error output. $conn = sqlsrv_connect($serverName, $connectionInfo); will just come out "false"
My setup :
'''
Windows 7 Enterprise 64 bit SP1
Wamp Version 2.5 (32bit)
Apache Version 2.4.9
PHP Version 5.5.12
MySQL Version 5.6.17
Microsoft SQL Server 2005 (on an external server)
extension=php_pdo_sqlsrv_55_ts.dll
extension=php_sqlsrv_55_ts.dll
'''
the PHP runs just fine in another server. That server however uses IIS.
$serverName = "first.second.third.fourth.fifth";
$connectionInfo = array("Database"=>"Databasename", "UID"=>"Username", "PWD"=>"password");
$conn = sqlsrv_connect($serverName, $connectionInfo);
if (!$conn) {echo "no connection to database/server";}
$sql = "SELECT * FROM Tablename";
$results = sqlsrv_query( $conn, $sql ); if($results === false) {print_r( sqlsrv_errors(), true);}
What I've tried so far :
checked that the server actually can be accessed. Same code works
fine on the IIS server.
tried to restore backups from previous PHP files I had.
checked the PHP.INI file(s) and checked that the sqlsvr extensions
are activated.
tried the same site on another wamp webserver.
Disabled AV to test if it's not maybe the connection/firewall to the SQL server.
checked that admin priv's are on the essential wamp exe's
What am I missing ? I truly think it's a small setting somewhere - just can't figure out where or what ! Maybe it's a setting that allows this system to connect to the SQL server ? This setting is enabled on the IIS server by default... ? Maybe I need to pass additional authentication somehow ?

Firstly, I used this to get the error message :
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}
Then the issue was it needed the ODBC Driver which I got from here:
https://www.microsoft.com/en-us/download/details.aspx?id=36434
Downloaded the 64 bit version and installed it and all was working again.

Related

PHP 5.6 + MS SQL connection on IIS 6.2 not working

I am trying to fix an error while trying to connect to MSSQL using PHP on my windows server running IIS.
The error that I am getting is - ODBC error.
Sorry can't copy the error text here and hence the image.
I have PHP 5.6 configured and working. The necessary dll files are there in the php extensions folder. They're also configured in the ini file.
Extensions used for the MSSQL connection are:
extension=php_sqlsrv_56_nts.dll
extension=php_pdo_sqlsrv_56_nts.dll
I am using the below sample code to connect to the mssql db:
<?php
$servername = "ServerName";
$conninfo = array ("Database" => "DBName", "UID" => "UserName" , "PWD" => "Password");
$conn = sqlsrv_connect($servername, $conninfo);
if($conn){
echo "Connection Established."
}else{
die(print_r(sqlsrv_errors(), true));
}
?>
The connection always gives me the error shown in the image attached.
I have been stuck with this for quite a few days and haven't been able to get this resolved.
Any help would be greatly appreciated.

Get undefined function sqlsrv_connect() in PHP

I am trying to use Microsoft server in a PHP program. I downloaded the Microsift drivers for PHP for SQL Server, made sure that Microsoft SQL Server 2012 Client was installed, made sure the extension_dir value was correct in my PHP.ini file, and added extension=php_sqlsrv_53_ts.dll in the dyanmic extension section of my PHP.ini file. Yet i still get the error 'Fatal error: Call to undefined function sqlsrv_connect()'. I am running an apache server on windows 7 and downloaded the 3.0 version of the driver.
So what am I doing wrong? What else should I check for? If I am running under an Apache server, I don't need to do anything to IIS, do I? Below is my PHP code.
<?php
// phpinfo(INFO_MODULES);
require_once 'serverlogin2.php';
/* Specify the server and connection string attributes. */
//$connectionInfo = array ("Database"=>$databaseName, "UID"=>$uid, "PWD"=>$pwd);
$connectionInfo = array ("Database"=>$databaseName);
/* Connect using SQL Server Authentication. */
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false )
{
echo "Unable to connect.</br>";
die( print_r( sqlsrv_errors(), true));
}
echo "made sql connection";
return;
?>
sqlsrv_connect (sqlsrv extension) is not available in the GA branch, you need to get it from svn and compile it. You can also use the mssql_connect (mssql extension) or PDO.

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.

PHP SQL Server driver not connecting

I'm using the Zend CE server with PHP 5.3.5 and Eclipse Helios with PDT to develop a database-driven PHP web application. I have my dev environment setup and enabled the SQL_Srv driver through the Zend control panel.
My problem is in my script when I call the sqlsrv_connect() function, it fails to connect to my SQL Server db and always returns false. I've verified I can connect to the server through a System DSN as well as Microsoft SQL Server Management Studio, so I know it's there and I can talk to it. These same settings also work on my coworker's dev environment (the same as mine), so I'm not sure what's missing. Has anyone else had similar issues with the PHP SQL Server driver? The relevant snippet of code:
$serverName = 'dbServer';
$connectionInfo = array ( "Encrypt" => 0, "LoginTimeout" => 10, "Database" => "Test_DB", "UID" => "ABC", "PWD" => "123" );
//This is always false. Does not connect
$test_Connect = sqlsrv_connect ( $serverName, $connectionInfo );
$GLOBALS['_connection'] = sqlsrv_connect ( $serverName, $connectionInfo );
if ( $GLOBALS['_connection'] == false ) {
print_r ( sqlsrv_errors(), true );
die ( "Failed to connect to SQL Server '$serverName'." );
}
Turns out PHP SQLSrv driver requires the Microsoft SQL Server 2008 Native Client to function. Make sure it's the '08' client and not '05', I already had the native client for SQL Server 2005 installed while experiencing this issue. It was only after I installed the client for SQL Server 2008 that it worked properly. Hope this helps others.

Trouble connecting to SQL Server with 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.

Categories