Connect To Mssql

Hi I have problem with connection.

This code works fine for me


$server = "";	

$link = mssql_connect($server, 'sa', 'xxx');

if (!$link) {

  die('Something went wrong while connecting to MSSQL');




  mssql_select_db("ALC", $link);

  // query or anyhting


but in Yii it is not working

here is my main.php


	'connectionString' => 'mssql:server=;database=ALC',

	'emulatePrepare' => false,

	'username' => 'sa',

	'password' => 'xxx',

	'charset' => 'utf8',



$cmd = Yii::app()->db->createCommand();

$cmd->select = 'Count(*)';

$cmd->from = 'base';        

$num = $cmd->queryScalar();

$this->render('index', array('data'=>$num));

here is an error

can anyone help me please?


I believe that "mssql" driver is problematic. I use the newer Microsoft Drivers 3.0 for PHP for SQL Server and connect successfully like so:


    'connectionString' => 'sqlsrv:server=localhost\\sqlexpress;database=MYDB',



    'nullConversion' => PDO::NULL_EMPTY_STRING,


Notice it uses the "sqlsrv" driver and also makes reference to the instance. YMMV…

At the time I am workin on Windows 7

Xampp version: 1.7.0

PHP: 5.2.8

but I have installed Windows Server 2008 R2 to another PC

Xampp version: 1.7.0

PHP: 5.2.8

and this works!!!

I think the problem is that I have php_pdo_mssql.dll extension is not right version for Windows 7. And I couldn’t find. Because this code give me same error. It is simple php code.



	$conn = new PDO("mssql:dbname=ALC;server=", 'sa', 'xxx');



catch(Exception $e)


	die( print_r( $e->getMessage() ) ); 


Same error

What does <?php phpinfo(); ?> say about the PDO drivers?

php_pdo_mysql works fine!!!

Do you have link that I can download php_pdo_mssql.dll which matches my requirments?

If phpinfo() says “Installed PDO Drivers — mssql, mysql, sqlite2” and you don’t have the .dll it probably means that it was compiled into the PHP executable. The link I provided above may have what you need in version 2.0 but since your PHP is so old it may no longer be supported.

I normally don’t use XAMPP but rather use the Windows native tools directly so I can’t help you much there; perhaps upgrading to a newer XAMPP will solve your problem.


I am trying to connect MS SQL server in my YII project.

i have done the modifications in main.php file as like below, also i have installed SQL srv PHP extension(php_sqlsrv_53_ts_vc9) in my WAMP stack.

‘connectionString’ => ‘sqlsrv:Server=hostname;Database=db_name’,

		'emulatePrepare' =&gt; false,

		'username' =&gt; 'user_name',

		'password' =&gt; 'password',

		'charset' =&gt; 'utf8'

i am getting the below error message:


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

Kindly help me to resolve the issue

What does <?php phpinfo(); ?> or the Yii requirements checker page say about the PDO drivers? Are they installed?

The other thing you might be missing is the DB instance on the connection string, as in ‘connectionString’=>‘sqlsrv:server=hostname\SQLEXPRESS;database=db_name’.