# MSSQL Unable to connect from console

Hi All,

I have to the following conecction string for both main.php and console.php:


<?php

return array(

'connectionString' => 'mssql:host=localhost\SQLEXPRESS;dbname=sp2000',

'charset' => 'utf8',

);



The WebApp is working perfectly, but Im unable to connect from the console, nor a custom console command nor with "shell index.php"

The error given is:

CDbConnection failed to open the DB connection: SQLSTATE[01002] Unable to connect: SQL Server is unavailable or does not exist. (severity 9)

Which is unclear to me since I can connect and query in the webapp (ie:SiteController)

Disclaimer: Im on windows 7 and using SQLSERVER EXPRESS 2008

Hi Asgaroth,

Just an idea, is the pdo driver for MSSQL enabled for CLI ? Or is its configuration ok ? (because I think that if it was a driver problem, you won’t have a such error message)

I don’t know for windows, but on several Linux distributions, the php.ini for command line is different than the one for apache/web server

Thanks tof,

The configuration for CLI and webapp its the same for windows: C:/Windows/php.ini

It has all been installed in default mode

( I hate windows when not gamming T_T )

Another stupid idea…

Do you have UAC enabled ? Did you try to run your php command line as administrator ?

I hate windows when … humm… thinking… … No, I hate windows Always

Yes im running it as an administrator, that was my first thought, I really dont know what to do, Ive never worked with MSSQL before T_T

And, since you are on Windows, did you try with sqlsrv driver instead of mssql ?

It seems that it’s newer and more supported than dblib on windows

Are you using a package like Wampp or Xampp ? or did you install php & webserver by yourself ?

This is the status:

This machine had AppServ, with php 5.2.6, and there was kind of a bug in that php version, so I changed AppServ for Wamp, which is using 5.3.10. Also using sqlsrv now, instead of mssql.

the original error is gone, but a new one (wierder) has arisen again when runnign from CLI. (the webapp still works though)


CDbConnection failed to open the DB connection: could not find driver



And well, the driver is there… or the webapp wouldnt be running. Ive ensured that the php.ini file is the same for both, webapp and phpcli.

any ideas? T_T

what does



\path\to\php -i



returns ? Check that pdo_sqlsrv is listed.

But. It’s very strange that it works in webapp, and not in CLI…

You are right, php -i doesnt show the extension enabled, even though its using the same php.ini

a phpinfo(); in the browser does show the extension, how can I enable the extension for CLI as well? I though that was what php.ini was for, and its alredy pointing to the same place. also forced it by running

php -c /path/to/php.ini mycmd

With the same unworking results

Does ‘php --ini’ command works on windows ?

It should show you all ini files that are parsed .

Thank you very much, for your help

You know how it works on windows, you reboot and It all works…

Yesterday:

web: php5/php.ini

cli: php5/php.ini

Today:

web: apache/php.ini

cli: php5/php-cli.ini

I replaced todays’s php.inis with yesterdays and now its all working

I hate windows

So, it was so simple !!!

Yesterday, you had AppServ, today Wamp. Seems that Wamp is now separating ini files for web and cli.

Me too, but often, you don’t have the choice. But fortunately, my employeer let me do what I want . So, Linux is install on my dev workstation, with a windows virtual box (needed Outlook 2010 on hosted Exchange, and also, to test website with this f***ing IE)