在PHP中将数组放到WHERE子句中,而不是X次Postgres [英] To put an array to a WHERE -clause in PHP without hitting X times Postgres

查看:120
本文介绍了在PHP中将数组放到WHERE子句中,而不是X次Postgres的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果不在PHP / PostgreSQL中创建50 pg_execute - 命令,如何将数组放置到WHERE子句中?

  //获取最新问题的question_ids 
$ 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());

//获取所有问题ids
$ question_ids = pg_fetch_all($ result_question_id);

//通过使用问题ids从db获取标题,question_id
$ result = pg_prepare($ dbconn,query8,SELECT title
FROM questions
WHERE question_id = $ 1 //这里的问题!如何获得许多WHERE的?
ORDER BY was_sent_at_time
DESC LIMIT 50;
);
$ result = pg_execute($ dbconn,query8,array($ question_ids));
//这里的问题,因为它是一个数组



我得到以下错误第一次

 警告: pg_execute()[function.pg-execute]:查询失败:ERROR:整数的输入语法无效:第22行的/var/www/codes/handlers/handle_questions_by_time.php中的Array
调用堆栈

$>


解决方案

subquery解决所有这些...

  $ 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;


How can you put an array to the WHERE -clause below without creating 50 pg_execute -commands in PHP / PostgreSQL?

     // 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

I get the following error for the line where I have the text Problem here first time.

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

解决方案

subquery solves all of this...

     $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;"

这篇关于在PHP中将数组放到WHERE子句中,而不是X次Postgres的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆