问题
I don't understand why CodeIgniter is wrapping my table name in brackets. When I use CodeIgniter's ODBC driver for MS SQL it shows the errors below, however it works perfectly using the MySql driver. How can I stop this error from occurring?
A Database Error Occurred
Error Number: 37000
[Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near ')'.
SELECT * FROM (ci_sessions) WHERE session_id = '3ad914bb5f5728e8ac69ad1db8fc9841' AND user_agent = 'Mozilla/5.0 (Windows NT 6.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2'
Filename: D:\wamp\www\IVR_Panel\system\database\DB_driver.php
Line Number: 330
I've also tried executing the same query in SQL Server 2005 which works without the brackets but still errors with them. I am using active record so I can switch between ODBC and MySQL drivers easily. Where can I modify CodeIgniter to remove the brackets?
回答1:
This is actually a bug in CodeIgniter. In the ODBC driver (/system/database/drivers/odbc/odbc_driver.php) when you select a table it uses the following method:
function _from_tables($tables)
{
if ( ! is_array($tables))
{
$tables = array($tables);
}
return '('.implode(', ', $tables).')';
}
It attempts to group multiple table selections together to enforce operator precedence, this should work fine if you are using more than one table, however with one table it still tries to group it which causes the error you are getting.
Unfortunately, I don't believe it's possible to extend these driver files so you may have to edit the core file itself. Take note of this in case you need to update CodeIgniter in the future, you will have to change the method to something like the following:
function _from_tables($tables)
{
if ( ! is_array($tables))
{
return strstr($tables, ',') ? '('.$tables.')' : $tables;
}
else
{
return count($tables) > 1 ? '('.implode(', ', $tables).')' : end($tables);
}
}
来源:https://stackoverflow.com/questions/9616091/codeigniter-and-odbc-connections