2013-10-10 65 views
0

我正在使用帶codeigniter的phpactiverecords。我想設置PDO :: ATTR_EMULATE_PREPARES假

我加入PDO::ATTR_EMULATE_PREPARES => false$PDO_OPTIONS文件../lib/Connection.php在line54。它導致未捕獲的異常:

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

有什麼想法嗎?

異常

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

堆棧跟蹤:

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 
+2

它會引起什麼異常? –

+0

我已經添加了異常和堆棧跟蹤。 – waqas

回答

2

的推薦方法設置連接編碼是連接DSN內charset屬性:

$dsn = 'mysql:host=localhost;dbname=foo;charset=utf8'; 
$conn = new PDO($dsn, 'jim', '12345678', $options); 

...但這個功能需要PHP/5.3.6或更高版本。您的庫可能會嘗試支持較舊的版本,以便運行SQL命令。細節是,你需要的正是這種運行:

SET names=utf8 

引用的說法是行不通的:

SET names='utf8' 

ERROR 1064 (42000): 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 ''

該庫是用準備好的發言運行查詢。下面是the offending code

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

由於utf8是一個字符串,它在技術上需要加引號。所以(理論上)你不能使用準備好的語句來運行這樣的查詢。在實踐中,如果你用模擬預處理語句它顯然能正常工作,但不是在您使用本機的:

<?php 

$dsn = 'mysql:host=localhost;dbname=test'; 
$user = 'test'; 
$pass = 'test'; 

// Works fine 
$pdo = new PDO($dsn, $user, $pass, array(
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, 
    PDO::ATTR_EMULATE_PREPARES => true, 
)); 
$res = $pdo->prepare('SET NAMES ?'); 
$res->execute(array('utf8')); 
unset($pdo); 

// Triggers SQL syntax error 
$pdo = new PDO($dsn, $user, $pass, array(
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, 
    PDO::ATTR_EMULATE_PREPARES => false, 
)); 
$res = $pdo->prepare('SET NAMES ?'); 
$res->execute(array('utf8')); 
unset($pdo); 

我的結論是,你的運氣了,如果你想使用這個庫,是:PHP ActiveRecord似乎不支持本地準備語句。

但是,它可能是一個簡單的補丁會做的伎倆:

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

編輯:我看過你的bug report。您也可以省略例外消息。請注意錯誤信息可以幫助你:你永遠不應該丟棄它們!

+0

你是完全正確的。問題出現在php-activerecord/lib/adapters/MysqlAdapter.php中。你提到的修復工作。 – waqas

相關問題