tags:

views:

58

answers:

2

How can you select the titles of the newest 3 questions from PostgreSQL database by PHP?

My attempt in PHP

$result = pg_prepare($dbconn, "query1", "SELECT title FROM 
    questions ORDER BY was_sent_at_time 
    WHERE question_id = $1;");
$result = pg_execute($dbconn, "query1", array(7, 1, 9);     
   // Problem HERE, since I do not know how you can get the 3 newest questions
+4  A: 

In MySQL it would look something like

SELECT title FROM questions ORDER BY was_sent_at_time DESC LIMIT 3;

Not sure if it still applies without changes to Postgres.

Alix Axel
Yes, PostgreSQL, MySQL, and SQLite support the LIMIT clause, though this syntax is not part of the SQL standard.
Bill Karwin
+2  A: 

Change your query to:

"SELECT title FROM questions WHERE question_id = $1 
ORDER BY was_sent_at_time DESC LIMIT 3;"
RaYell