2013-04-28 58 views
1

我试过在PDO中禁用模拟准备,但我无法让它工作。其他一切正常。查询成功。我认为它不工作的原因是因为它不能避免引号,所以我得到语法错误。将PDO :: ATTR_EMULATE_PREPARES设置为false不起作用

我试过两种不同的方法。

$this->dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); 

$insert = $database->$con->prepare($insert, array(PDO::ATTR_EMULATE_PREPARES => false)); 

我也注意到的getAttribute不起作用。

通过这样做......

$emul = $database->$con->getAttribute(PDO::ATTR_EMULATE_PREPARES); 
    var_dump($emul); 

...我得到这个错误

SQLSTATE[IM001]: Driver does not support this function: driver does not support that attribute 

这是我的数据库类,其中动作发生。 (我可能会留下一些不必要的/愚蠢的代码在那里,而我测试。)

<?php 
class Database 
{ 
    public $dbh; 
    public $dbh1; 
    public $dbh2; 
    private static $instance; 

    public $numResults; 
    private $result = array();   // Results that are returned from the query 

    public function __construct() 
    { 
     try 
     { 
      $this->dbh = new PDO(DB_TYPE.':host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8', DB_USER, DB_PASS); 
      $this->dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); 
      $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

      $this->dbh1 = new PDO(DB_TYPE1.':host='.DB_HOST1.';dbname='.DB_NAME1.';charset=utf8', DB_USER1, DB_PASS1); 
      $this->dbh1->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); 
      $this->dbh1->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

      $this->dbh2 = new PDO(DB_TYPE2.':host='.DB_HOST2.';dbname='.DB_NAME2.';charset=utf8', DB_USER2, DB_PASS2); 
      $this->dbh2->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); 
      $this->dbh2->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
     } 
     catch (PDOException $e) 
     { 
      die("Database Error: ". $e->getMessage() . "<br />"); 

     } 
    } 

    public static function getInstance() 
    { 
     if (!isset(self::$instance)) 
     { 
      $object = __CLASS__; 
      self::$instance = new $object; 
     } 
     return self::$instance; 
    } 

    private function tableExists($table, $con) 
    { 
     switch($con) 
     { 
      case 'dbh': 
      $db_name = DB_NAME; 
      break; 
      case 'dbh1': 
      $db_name = DB_NAME1; 
      break; 
      case 'dbh2': 
      $db_name = DB_NAME2; 
      break; 
     } 

     $database = Database::getInstance(); 

     if(is_array($table)) 
     { 
      for($i = 0; $i < count($table); $i++) 
      { 
       $tablesInDb = $database->$con->prepare('SHOW TABLES FROM '.$db_name.' LIKE "'.$table[$i].'"'); 
       $tablesInDb->execute(); 
       $rowCount = $tablesInDb->rowCount(); 

       if($tablesInDb) 
       { 
        if($rowCount <> 1) 
        { 
         die('Error: Table does not exist'.$table[$i]); 
        } 
       } 
      } 
     }else 
     { 
      $tablesInDb = $database->$con->prepare('SHOW TABLES FROM '.$db_name.' LIKE "'.$table.'"'); 
      $tablesInDb->execute(); 
      $rowCount = $tablesInDb->rowCount(); 

      if($tablesInDb) 
      { 
       if($rowCount <> 1) 
       { 
        die('Error: Table does not exist'.$table); 
       } 
      } 
     } 

     return true; 
    } 

    public function insert($con, $table, $values, $cols = null) 
    { 
     if($this->tableExists($table, $con)) 
     { 
      $insert = 'INSERT INTO '.$table; 

      if($cols != null) 
      { 
       $cols = implode(',', $cols); 
       $insert.= '('.$cols.')'; 
      } 

      for($i = 0; $i < count($values); $i++) 
      { 
       if(is_string($values[$i])) 
        $values[$i] = "'".$values[$i]."'"; 
      } 

      $values = implode(',', $values); 
      $insert .= ' VALUES ('.$values.')'; 

      $database = Database::getInstance(); 
      $insert = $database->$con->prepare($insert, array(PDO::ATTR_EMULATE_PREPARES => false)); 
      $insert->execute(); 

      if($insert) 
      { 
       return true; 
      }else 
      { 
       return false; 
      } 
     } 
    } 

    public function getResult() 
    { 
     return $this->result; 
    } 
} 

?> 
+0

语法错误? ** [请教一个关于这个问题的问题](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem),而不是基于它的假设。** – 2013-04-28 16:13:44

+0

那么,这一切都取决于'DB_TYPE'是什么......并不是每种类型都可能准备好语句。 – Wrikken 2013-04-28 16:21:37

+0

@Wrikken DB_TYPE是mysql。 – Riketh 2013-04-28 16:22:19

回答

1
  1. 作为manual状态,getAttribute()不支持ATTR_EMULATE_PREPARES
  2. 没有不宜与本地没有准备逃脱在所有。
  3. 要检查您是否处于仿真模式,您可以使用LIMIT clause with lazy binding。如果仿真打开,它会引发错误。
  4. 你的主要问题是你提到的任何“语法错误”,你必须先解决它。
  5. 正如ÁlvaroG. Vicario在评论中指出的那样,您没有使用准备好的语句。这显然是问题的根源。 PDO本身不会“逃避”你的数据。只有在您使用占位符来代表您在查询中的数据时,才能执行此操作。您可以阅读更多here
相关问题