Connect To Mssql

Hi I have problem with connection.

This code works fine for me

C:/xampp/htdocs/someProject/index.php




$server = "122.212.161.80";	

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

if (!$link) {

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

}

else

{

  mssql_select_db("ALC", $link);

  // query or anyhting

}



but in Yii it is not working

here is my main.php




'db'=>array(

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

	'emulatePrepare' => false,

	'username' => 'sa',

	'password' => 'xxx',

	'charset' => 'utf8',

),



SomeController.php




$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?

thanks

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:




'db'=>array(

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

    'username'=>'sa',

    'password'=>'xxx',

    '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.




try

{			

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

	$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

}

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.

Hi,

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:

CDbException

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’.