Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework 2 Microsoft SQL Server 2008 DB connection

I tried out Zend Framework 2 skeleton application and its working fine in Zend Server 5.6 (PHP Version 5.4.0 apache 2.2.21 MYSQL 5.0.10). But i want Zend Framework 2 to connect with MS SQL 2008. I tried the following but it doesn't work and throws exception " An invalid parameter was passed to sqlsrv_execute. "

'db' => array(
    'driver'    => 'sqlsrv',
    'hostname'  => 'testserver\test',
    'Database'  => 'payroll',
    'UID'       => 'sa',
    'PWD'       => '123456'
),

whats wrong with above db array? please suggest me with correct connection string

FYI :

i have tested php 5.4 and MS SQL 2008 connection and it works fine, the following connection was established successfully.

/*
$serverName = "testserver\test"; //serverName\instanceName
$connectionInfo = array( "Database"=>"payroll", "UID"=>"sa", "PWD"=>"123456");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
    echo "---------- Connection established --------------------.<br />";     
    $sql = "select * from users";
    $stmt = sqlsrv_query($conn, $sql);
    while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
          echo $row['id'].", ".$row['username']."<br />";
    }    
} else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}
*/
like image 556
user1692442 Avatar asked Sep 24 '12 09:09

user1692442


People also ask

How do I connect to a Microsoft SQL database?

Connect to a SQL Server instanceStart SQL Server Management Studio. The first time you run SSMS, the Connect to Server window opens. If it doesn't open, you can open it manually by selecting Object Explorer > Connect > Database Engine. For Server type, select Database Engine (usually the default option).

How connect MS SQL with PHP?

Running MS SQL Queries from PHP See the full list of MS SQL functions in PHP at php.net. The following script is an example of an MS SQL SELECT query run from PHP. <? php $query ="SELECT col1,col2 FROM tablename"; $result =mssql_query($query); while ( $record = mssql_fetch_array($result) ) { echo $record["col1"] .

Does PHP work with MS SQL?

The Microsoft Drivers for PHP for SQL Server enable integration with SQL Server for PHP applications. The drivers are PHP extensions that allow the reading and writing of SQL Server data from within PHP scripts.


1 Answers

As you did not technically "answered" your own question, I will do it for you.

Try these connection parameters, they might work using PDO :

'db' => array(
    'driver'    => 'pdo',
    'dsn'       => 'sqlsrv:database=payroll;Server=testserver\test',
    'username'  => 'sa',
    'password'  => '123456'
),
like image 110
MaxiWheat Avatar answered Nov 10 '22 00:11

MaxiWheat