jpfuentes2 / php-activerecord

ActiveRecord implementation for PHP
http://www.phpactiverecord.org/
Other
1.32k stars 443 forks source link

Error on setting PDO::ATTR_EMULATE_PREPARES to False in $PDO_OPTIONS #367

Open waqasajaz opened 10 years ago

waqasajaz commented 10 years ago

I am adding PDO::ATTR_EMULATE_PREPARES => false to $PDO_OPTIONS array in file ../lib/Connection.php at line54. It is causing uncaught exception at:

../lib/adapters/MysqlAdapter.php(76): ActiveRecord\Connection->query('SET NAMES ?', Array)

exception:

Fatal error: Uncaught exception 'ActiveRecord\DatabaseException' with message '42000, 1064, You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1' in /home/waqas/apps/FBI-PHP/sparks/php-activerecord/0.0.2/vendor/php-activerecord/lib/Connection.php:304

Stack Trace:

0) /home/waqas/apps/FBI-PHP/sparks/php-activerecord/0.0.2/vendor/php-activerecord/lib/adapters/MysqlAdapter.php(76): ActiveRecord\Connection->query('SET NAMES ?', Array)

1) /home/waqas/apps/FBI-PHP/sparks/php-activerecord/0.0.2/vendor/php-activerecord/lib/Connection.php(108): ActiveRecord\MysqlAdapter->set_encoding('utf8')

2) /home/waqas/apps/FBI-PHP/sparks/php-activerecord/0.0.2/vendor/php-activerecord/lib/ConnectionManager.php(33): ActiveRecord\Connection::instance('mysql://root:@l...')

3) /home/waqas/apps/FBI-PHP/sparks/php-activerecord/0.0.2/vendor/php-activerecord/lib/Table.php(103): ActiveRecord\ConnectionManager::get_connection(NULL)

4) /home/waqas/apps/FBI-PHP/s in /home/waqas/apps/FBI-PHP/sparks/php-activerecord/0.0.2/vendor/php-activerecord/lib/Connection.php on line 304

any idea?

waqasajaz commented 10 years ago

line 73 of php-activerecord/lib/adapters/MysqlAdapter.php contains offending code. It is directly executing query instead of preparing it.

public function set_encoding($charset)
{
    $params = array($charset);
    $this->query('SET NAMES ?',$params);
}

changing above function to below one resolves the issue.

public function set_encoding($charset)
{
    $this->query('SET NAMES ' . $charset));
}

For more details see http://stackoverflow.com/a/19291558/649388

tuupola commented 10 years ago

Above fix breaks the build though.

%php -v
PHP 5.3.26 (cli) (built: Jul  7 2013 19:05:08) 
Copyright (c) 1997-2013 The PHP Group

...

1) MysqlAdapterTest::test_set_charset
PDOStatement::execute() expects parameter 1 to be array, string given

/Users/tuupola/Code/php/php-activerecord/lib/Connection.php:320
/Users/tuupola/Code/php/php-activerecord/lib/adapters/MysqlAdapter.php:76
/Users/tuupola/Code/php/php-activerecord/lib/Connection.php:117
/Users/tuupola/Code/php/php-activerecord/test/MysqlAdapterTest.php:24
waqasajaz commented 10 years ago

@tuupola thanks for the test. Can you suggest a fix for this issue please. Let me admit that I have no idea what this issue is all about. It 'll be great if you can guide me how to run its test cases.