2009-08-14 7 views
0

PHP/PostgreSQLで50 pg_executeコマンドを作成せずに配列をWHERE節に入れるにはどうしたらいいですか?配列をPHPでX時間を掛けずにPHPのWHERE節に入れるにはPostgres

 // to get the question_ids of the newest questions 
    $result_q_id = pg_prepare($dbconn, "query6", "SELECT question_id 
     FROM questions 
     ORDER BY was_sent_at_time 
     DESC LIMIT 50;" 
    ); 
    $result_question_id = pg_execute($dbconn, "query6", array()); 

    // to get all question ids 
    $question_ids = pg_fetch_all($result_question_id);        

    // to get titles, question_id from the db by using the question ids 
    $result = pg_prepare($dbconn, "query8", "SELECT title 
     FROM questions 
     WHERE question_id = $1   // Problem here! How to get many WHERE's? 
     ORDER BY was_sent_at_time 
     DESC LIMIT 50;" 
    );                              
    $result = pg_execute($dbconn, "query8", array($question_ids)); 
      // Problem here, since it is an array 

私は、テキストProblem here最初の時間を持っている行の次のエラーを取得します。

Warning: pg_execute() [function.pg-execute]: Query failed: ERROR: invalid input syntax for integer: "Array" in /var/www/codes/handlers/handle_questions_by_time.php on line 22 
Call Stack 

答えて

3

サブクエリは...形式の修正のための

 $result = pg_prepare($dbconn, "query8", 
"SELECT title   
FROM questions   
WHERE question_id in (SELECT question_id 
      FROM questions 
      ORDER BY was_sent_at_time 
      DESC LIMIT 50)     
ORDER BY was_sent_at_time   
DESC LIMIT 50;" 
+0

おかげでこのすべてを解決:) –

+0

はどうもありがとうございました! - 私はこの素晴らしいテクニックを完全に忘れていた。 –

関連する問題