How can you get many tags for a question effectively from Postgres database by PHP?
I have tables tags and questions where I have the variables questions_question_id and question_id, respectively, for instance.
One question may have many tags.
I can select question_id, title and a tag from the tables.
However, I do not know how to get many tags for one question out of the database effectively. PHP restricts me in manipulating the data as matrices.
I can read question_id, title and tag to the variable $result.
The data is a matrix for me in the variale $result.
However, I cannot refer to the data like to a matrix.
I have only found the functions pg_fetch_row and pg_fetch_all_columns in PHP. The former gets the data as arrays for rows, while the latter as arrays for columns.
My problem with LEFT JOIN suggests me that it may not be possible to get the data as follows
- get the columns for title, question_id and tag
- group the data by question_id such that I can put tags to my questions: I have tried to manipulate the data by PHP, but I get only this far.
I think what you want is a second query that gets all the tags for a given question. Trying to join them together is gonna be cumbersome. It can be done but you’ll end up with several rows with the same question data but different tag data.
So first do something like this (I don’t know the structure of your database so this is just an example):
Then you do a query right after that one, which asks for all the tags: