简体   繁体   中英

Error executing Queries in SQL Server Native Client 10.0

I'm recently began to use php again (last time was like 7 years ago) and some fiends recommended me to use codeigniter Framework.

i trying to connect to database server in MSSQL 2008 R2 using a the odbc driver, using x64 php version over x64 IIS. This is one of the reasons because i'm not using the php native driver provided by microsoft, the other one is because the production enviroment will be in CentOS, and btw doesn't have any sense to use that driver ....

going to the point: Trying to connect to a database using active record / odbc configuration throws me this error

A Database Error Occurred
--------------------------
Error Number: 01000
[Microsoft][SQL Server Native Client 10.0][SQL Server]Executing SQL directly; no cursor.
SELECT * FROM (WebUsers)
Filename: C:\projects\php\test\system\database\DB_driver.php
Line Number: 330

and i'm only execute a simple query to retrieve a table.

$query = $this->db->get('WebUsers');
return $query->result_array();

these are my connection settings:

$db['default']['hostname'] = 'Driver={SQL Server Native Client 10.0};Server=localhost;Database=XXXXXXXXX;';
$db['default']['username'] = 'WWWWWW';
$db['default']['password'] = 'YYYYYY';
$db['default']['database'] = 'XXXXXXXXX';
$db['default']['dbdriver'] = 'odbc';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

DOn't have any idea how to solve it

I think this is an old bug in CI. You have to modify a function _form_tables() in /system/database/drivers/odbc/odbc_driver.php

function _from_tables($tables)
{
    if ( ! is_array($tables))
    {
        return strstr($tables, ',') ? '('.$tables.')' : $tables;
    }
    else
    {
        return count($tables) > 1 ? '('.implode(', ', $tables).')' : end($tables);
    }
}

Hope this will work.

$this->db->query($sql);     

而不是上面的查询使用下面的方法来执行codeigniter中的执行过程

 $this->db->simple_query($sql);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM