1

Upgraded a (dev) Cake PHP site and had to change database drivers from ADO to SQL Server's own driver as ADO is no longer supported in Cake 1.3, I . The new SQL Server driver is installed in PHP, shows in PHPinfo() and should work, but when I try to load a page using the database, I get this error:

Warning (2): sqlsrv_query() expects parameter 1 to be resource, boolean given 
[APP\plugins\datasources\models\datasources\dbo\dbo_sqlsrv.php, line 184]
Warning (512): SQL Error: An invalid parameter was passed to sqlsrv_query.
 [CORE\cake\libs\model\datasources\dbo_source.php, line 684]
Query: SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES 

What appears to be happening is my connection isn't established in the dbo_sqlsrv.php driver; a "$this->connection" variable is supposed to be the connection resource then passed to sqlsrv_query() and it's apparently a bool which is wrong (I tried to Echo the variable and it displays nothing).

This is where the connection SHOULD be set:

sqlsrv_connect($this->config['host'] . $port, $params)

Printing the variables that were passed in gives me this:

SRV, 1433 Array ( [Database] => DB [CharacterSet] => char 
[MultipleActiveResultSets] => [UID] => sa [PWD] => password )

Each of those parameters is correct, is there a specific way I should format or change my database configuration array for this driver or is there something I am missing?

2 Answers 2

2

This error was due to outdated SQL Server drivers. Installing the latest from below allows a SQL Server connection:

http://www.microsoft.com/download/en/details.aspx?id=20098

Sign up to request clarification or add additional context in comments.

Comments

1

I think there is some issue with your connection. PHP can't establish connection with sql Server.

var $default = array(
        'driver' => 'sqlsrv',
        'persistent' => false,
        'host' => 'Myhome-PC\SQLEXPRESS', // or ip-address(192.168.1.13)
        'login' => 'username',
        'password' => 'password',
        'database' => 'db',
        'prefix' => 'tbl',
        'port' => NULL,
        'returnDatesAsStrings' => True
    );

I think the Connection should be this

Here is the sqlsrv_dbo which I used.

And your $param should be something like this

$connectionInfo = array("UID" => $config['login'], 
                        "PWD" => $config['password'], 
                        "Database" => $config['database'],   
                        'MultipleActiveResultSets' => 1,
                        'ConnectionPooling' => 0
);
if(isset($config['ReturnDatesAsStrings'])){
         $connectionInfo['ReturnDatesAsStrings'] = $config['ReturnDatesAsStrings'];
}
$this->connection = sqlsrv_connect($config['host'] . $port, $connectionInfo);

2 Comments

That's exactly what it's doing though, I'm not configuring port info but it's using the default port which is right. your last block of code is almost exactly what's going on in the sqlsrv.php driver file.
Have You tried to connect the sql server from simple php, I mean without framework?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.