php cli mssql_connect - Unable to connect: SQL server is unavailable - php

Im trying to run a shell with cakephp but I'm not able to do it due to a SQL connection error, I'm always getting this
Warning:mssql_connect(): Unable to connect to server: XXXXX I'm sure the config for the data source is correct as I'm able to connect using the website side of the app.
Have anyone stumble with something like this?
BTW the webserver and the mssql server are not the same, they are on different hosts.
Environment Info ( I agree is a mess... ):
Web server
Windows server 2003 R2
PHP-5.2.4
IBM http server 6.1
Cakephp 1.2
DB Server
Windows server 2003 R2
Microsoft SQL server 2005
I have isolated the problem and found is not cakephp related, I created a script with just this line of code
mssql_connect('XXXX', 'YYYY', 'ZZZZ', true);
Giving me the same error. This should be something different between CLI and how php is run from the HTTP server.
Thank you!

Make sure you specify the port to connect to (usually 1433). If that still doesn't work its possible you need to play with named pipes settings. More information can be found in the comments on the PHP site: http://ca3.php.net/function.mssql-connect
I would also suggest you look into using the Microsoft SQL Serv drivers for PHP, they are much better maintained than the mssql ones. http://www.microsoft.com/download/en/details.aspx?id=20098 Here is an article explaining the differences: http://blogs.msdn.com/b/brian_swan/archive/2010/03/08/mssql-vs-sqlsrv-what-s-the-difference-part-1.aspx If you do go the sqlsrv route (and I really suggest you do) here are the connection parameters: http://php.net/manual/en/function.sqlsrv-connect.php Use example 3.

Here are the steps I followed to narrow and then fix the problem:
Check which php.ini is your cli using by running the next command: php -i
Isolate the problem, I was thinking it was something with cakephp, but at the end every problem is related to a single line in php, my problem was around this line of code mssql_connect('XXXX', 'YYYY', 'ZZZZ', true);
Based on the info provided here I found out that the next dll was outdated or not compatible with Windows Server 2003, just get ntwdblib.dll ver. 2000.80.2039.0, use this exact version as others like ver.2000.80.194.0 didn't worked for me.
As #charles mentioned if you are not working already in a project already deployed use sqlsrv drivers as those are maintained currently and the mssql are basically getting deprecated.
Thanks!

Related

getting intermittent ORA-12545 error

I am running a monitoring site developed in yii framework using xampp on a windows server machine to check the health of databases. Normally everything works fine but sometimes it gives me the following error:
Code: 12545|Message: CDbConnection failed to open the DB connection: SQLSTATE[HY000]: pdo_oci_handle_factory: ORA-12545: Connect failed because target host or object does not exist (ext\pdo_oci\oci_driver.c:635)
I check ping and everything is fine, tns_ping is working, other users can logon to database and use it. but i get the above error.
PHP Version 5.5.28
Oracle9i Enterprise Edition Release 9.2.0.8.0 - 64bit Production
Oracle Instant Client - 11.2.0.1.0
Windows Server 2012 R2 Standard
PDO Driver for OCI 8 and later enabled
Query used to check if we can connect to database and query it
select count(*) from dual
any help will be appreciated
I assume you are using yii 1 but nevertheless you should try those steps in fixing the error:
In most cases defining the database in your main.php fixes the problem, so try adding: 'connectionString'=>'oci:dbname=mydb',
Disable those extensions: php_pdo_oci8.dll and php_oci8.dll in your php.ini file and leave only the needed one for your environment: php_pdo_oci.dll.
Definitely update your Oracle to 10 or above because according to my experience and some internet forums Oracle client 9 can make some troubles with php 5.1 or above. Not in every scenario but I think it's worth the update.

Connect to a remote Windows Sybase database from Ubuntu via PHP

Ubuntu 12.04 LTS Precise
I'm trying to use PHP to connect to a remote Windows Sybase database. Let us assume Windows 7 Enterprise SP1. I'm currently doing this in PHP:
$db = sybase_connect("10.1.208.111", "$user", "$pass");
And I'm getting an error of:
Warning: sybase_connect(): Unable to connect to server: 10.1.208.111
I understand that the server I'm connecting to (10.1.208.111), needs to be in the interfaces file. I'm trying to figure out where that is, or even if I have one on the Ubuntu server (I'm still new to linux itself). I read that the path for the file is located in the SYBASE environmental variable. I don't know where that is either. Can anyone shed some light? I can offer more information if you need it, I just don't know what you need right off the bat.
PS: The windows machine has Micros RES installed on it. That's the database I'm trying to pull data from. Not sure if that matters.
The default installation directory for Sybase ASE in windows is C:\sybase, so your interfaces (interfaces.ini) should be located there.
For SQL Anywhere, the interfaces file is called SQL.ini, and it's location varies, but you should be able to do a search of your system drive to find it.
For SQL Anywhere you probably want to use the PHP apis that are provide by SAP Sybase. The SQL Anywhere documentation is a pretty good place to start, as it covers all the pieces that need to be installed to get SQL Anywhere to talk to PHP.
SQL Anywhere 11: SQL Anwhere PHP API
You may also be able to find good information over at the SAP Community Network site for SQL Anywhere.

Laravel 4 on Mac localhost: Cannot find driver for SQLSRV

I'm trying to connect with Laravel 4 to an SQL Server database. I'm getting an error, but I think it's a PHP error, not a Laravel error. It says PDOException: could not find driver. I have the driver set to sqlsrv like another example in the database.php file. Like I said, I think this is a PHP error, not Laravel.
I'm on localhost on my Mac. Do I need to configure something in my php.ini file? I found another article that said I need to comment out a MySQL driver under PDO. I also couldn't find that line in my php.ini. But I need to connect to a MySQL database as well, anyway. What do I need to change in my php.ini file?
I found this line:
pdo_mysql.cache_size = 2000
But commenting it out did nothing even after restarting Apache.
The sqlsrv driver is a windows-only driver according to the PHP Manual (www.php.net/manual/en/sqlsrv.requirements.php). So you can't use that driver on Mac, Linux, Unix or any other operating system than the ones listed on that page.
I assume you are trying to connect to a remote SQL Server somewhere? If you're on Mac, then SQL Server wouldn't be running on your local machine, so I guess the first question is whether or not you have a SQL Server running somewhere.
Second, from the server that is running PHP, you would need to connect using either FreeTDS or ODBC with FreeTDS.
Since Laravel 4 uses PDO, you'll need to install FreeTDS and ODBC. Laravel 4 doesn't use the mssql PHP extension. It uses PDO, so mssql won't get you anywhere with Laravel 4.
If you're serving PHP from your Mac, I would say there are generally 4 different ways you could go:
The Easy, but costly way: Go to (www.macupdate.com/app/mac/23152/openlink-odbc-driver-for-sybase) and download and install OpenLink ODBC driver. This allows you to create new ODBC connections using the Sybase (MSSQL) driver on a XNIX machine. There's an installation walk-through here (wikis.openlinksw.com/dataspace/owiki/wiki/UdaWikiWeb/InstallSybaseExpressOSX). It comes with a 15 day trial, after which you would have to purchase it. As of this writing, it appears to cost somewhere in the range of $30 - $50 depending on the license you choose.
The harder, but free way: Follow http://blog.benjaminwalters.net/?p=10 (slightly outdated) to re-compile FreeTDS support into PHP on Mac (assuming you're using MAMP)
The far better, but also far more work way: Find a Virtual Machine Host software like VMWare Fusion and install a Linux server like Ubuntu 12.4. Make sure you install apache2 and PHP during the install process and then follow the steps here to get FreeTDS working on your new server machine.
Run PHP from the Windows Server that is running SQL Server
Milage will vary with each solution, and there is a learning curve in any case. I've been a PHP developer for a long time, and it took me days to figure out how to get connected to SQL Server for the first time from Linux. Thankfully, there are quite a few walkthroughs out there now, and FreeTDS has come a long way since that time.
Hope that helps. :)
I actually have this running on my setup. My best advice is to get on the Homebrew bus.
https://github.com/josegonzalez/homebrew-php is where you're going to want to look for installing PHP and related items. I don't remember exactly what I did but it wasn't particularly difficult.
First you'll need FreeTDS.
brew install freetds
Then, add Jose's formulas to your homebrew (instructions on the github page). After that,
brew install php5x --with-mssql
brew options php5x
if you need more extensions.
Based on Jon Watson's comment,
I've tried step 2, and it doesn't work. check my issue here https://github.com/laravel/laravel/issues/2180
Step 3 is broken link and I think it's the same with step 2.
I also tried linux ODBC driver for Laravel (there are 2, google it), both doesn't work too. Although my simple test php works in both DBLIB and ODBC (compiled using official microsoft odbc unix driver).
Step 4 definitely works, BUT you must use IIS for best stability.
That leaves step 1. OR,
Ditch Laravel4 and use good ol' AdoDB http://adodb.sourceforge.net/ which I've been using since 2006 and still working superbly (although not modern like Laravel) configure it using tutorial in http://docs.moodle.org/20/en/Installing_MSSQL_for_PHP
I wonder why they don't build sqlsrv driver for linux too....

How to connect to a remote MSSQL database with PHP on Linux?

I've got a problem and there doesn't seem to be a strait forward solution.
I'm running a Linux server with PHP and need to connect to a MSSQL database on a remote server (I don't have control over this server). Speaking to my host, they say that it's not possible eto install the MSSQL extensions on a linux box as they are only available on Windows Servers. So, I'm looking to see what other options i have.
Any ideas how I can do this on a linux box?!
it's not possible eto install the MSSQL extensions on a linux box
They lied. It is possible, but it requires FreeTDS to compile.
Other solution is to use other host (with windows) to act as proxy to MSSQL server, and communicate with it by SOAP/REST
you have to do it via an ODBC connection.. odbc_connect() etc etc.. Microsoft has drivers for this http://www.microsoft.com/en-gb/download/details.aspx?id=28160.. (edit) I have done this without any of the issues posted... the php.net website for odbc_connect even gives mSSQL examples in the manual http://php.net/manual/en/function.odbc-connect.php
Maybe this helps, send it to your server host:
http://pointbeing.net/weblog/2010/05/successful-microsoft-sql-server-support-for-php-on-linux.html
Commonly a database module is installed but in your case you could try to use a REST-API to talk with the MSSQL database. But that must be installed on a second server e.g. the database server itself.

php odbc_connect to mssql works in linux/ubuntu command line not browser

I really feel like I have done an exhaustive search on this matter, found similar issues and have tried a number of things without success, so I am for the first time posting a question here:
I do a lot of web pages/web programming (PHP/jQuery mostly) and host these pages on my digital library department's Drupal/Ubuntu Server. I just code them and post them via Dreamweaver, as I don't have much use for the Drupal front end. I just need the server for my pages.
Anyway, I am wanting to make some PHP pages that can connect to our library catalog, so I have been working from the command line of the Drupal/Ubuntu server. I installed unixODBC, had some troubles, installed FreeTDS, set up the drivers and the DSN.
Finally...
I was able to get a connection to our catalog via tsql and run a query.
I made a basic PHP script to test the connection:
It works from the Ubuntu command line of the server (connection successful).
It doesn't work when I load it in a browser. I get the dreaded:
Warning: odbc_pconnect(): SQL error: [unixODBC][Driver Manager]Data source name not found, and no default driver specified, SQL state IM002 in SQLConnect in /var/www/mydirectory/mytestpage.php on line 15
UPDATE: I installed a test Ubuntu Server in a partition on my laptop, where I started clean, installed Apache2/PHP5(with mssql and odbc modules, etc)/FreeTDS, etc. I got a tsql connection. I made the basic PHP script and ran it successfully from the command line. Then I navigated to the php page from a browser and it worked! I still need to get this to happen on the Drupal server version, but now I have at least narrowed down the issue to something on the Drupal server.
I searched and found such possible causes as:
File permissions, which I think I got corrected, but no go
That maybe I had set up a User DNS and not a System DNS which I tried my best to correct with the proper "odbcinst" command options
That maybe php needs to be running in CGI mode instead of as a module. But that seemed unlikely from what I read. And I am not sure how that would affect Drupal. (There are limits to the system wide changes I should make here.)
A number of people said it was SElinux blocking Apache from displaying the page in the browser, but temporarily disabling SElinux didn't work.
Thanks for any assistance!
Check you have enabled the mssql.so etc at the following location
/etc/php5/apache2/php.ini, you will be having another at /etc/php5/cli/php.ini
in ubuntu php.ini will be maintained separately for cli & mod_php apache configuration
If you are going to test your odbc sources on the commandline, I would advise you use the isql utility instead of tsql. isql uses the DSN you defined in odbc.ini to connect to the database, while tsql connects directly to your database.
If it works with isql, it should work on your webpage as well.
Also check the Drupal/Ubuntu Server files /etc/odbc.ini (your DSN sources) and /etc/odbcinst.ini (TDS Driver settings) to see if they match the ones on the working Ubuntu installation.
My guess is it has something to do with the php. ini file. On my Ubuntu system I've got 2.
One in /etc/php5/apache2/php.ini
And one in /etc/php5/cgi/php.ini
The values in the apache2 path will need to be adjusted to allow the connection. Unfortunately outside of that I'm not too much use as I use MySQL and Mongo
Copy your php.ini from work server to non-work server, add make needed environment variables accessible before starting your web server, and make sure your web-server has full access to your odbc and odbcinst ini-files. And so, using mssql knowledge base or help.

Categories