2012-02-27 20 views
0

私はちょうどselectステートメントのために2時間を費やしました。文字列を含むzend selectステートメント

私は間違っていますか?

public function fetchSpecific($moderatorid = 1) 
    { 

     $resultSet = $this->getDbTable()->fetchAll("moderatorid = ".$moderatorid); 

     $entries = array(); 
     foreach ($resultSet as $row) { 
      $entry = new Application_Model_Network(); 
      $entry->setId($row->id) 
        ->setName($row->name) 
        ->setModeratorId($row->moderatorid); 
      $entries[] = $entry; 
     } 
     return $entries; 
    } 

これは、すべての行についてです:

$resultSet = $this->getDbTable()->fetchAll("moderatorid = ".$moderatorid); 

それは私にエラーを与えるが、これを好き:

Message: SQLSTATE[42000]: Syntax error or access violation: 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 

答えて

2

技術的な構文が働くべきだと、私はそれを変更することをお勧めしますが、データが適切にエスケープされていることを確認してください。そのエラーが発生したときに$moderatoridにはどのような価値がありますか?何らかの理由で変数が構文エラーを生成していると思われます。

代わりにこれを試してみてください:

$results = $this->getDbTable() 
       ->fetchAll($this->getDbTable() 
           ->getAdapter() 
           ->quoteInto('moderatorid = ?', $moderatorid)); 

これは$moderatoridが適切にエスケープ取得し、構文エラーを防ぐのを助けるべきであることを確認します、そしてさらに重要なことは、可能なSQLインジェクションを防ぐことができます。

Zend_Db_Table::fetchAll()を使用した場合のその他の問題は、このようなエラーが発生した場合、実行中のクエリをデバッグすることが難しいことです。

これを回避するには、SELECTステートメントを自分で作成します。実行されている実際のSQLをデバッグする必要がある場合は、値をエコーバックできます。

$select = 
$this->getDbTable() 
    ->select() 
    ->from($this->getDbTable()) // optionally you can specify cols as the 2nd param 
    ->where('moderatorid = ?', $moderatorid); 

echo $select; // e.g. SELECT `table`.* FROM `table` WHERE (moderatorid = 1) 

$results = $this->getDbTable()->fetchAll($select); 

あなたの問題を解決するのに役立ちます。

いくつかの有用な参考文献:
Zend_Db_Select
Zend_Db_Table

+0

ニース説明。 +1 – RockyFord

+0

これは私の人生を保存しました.. – Proto

+0

優秀、喜んで助けて! – drew010

関連する問題