What I have:
I have a table in MySQL named "updates" that currently holds the following information:
What I need:
What I need is the following:
What I have done so far:
I have the following MySQL query that works:
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(IF(Date = ''',
Date,
''', Description, NULL)) AS ',
CONCAT("'",Date,"'")
)
) INTO @sql
FROM updates;
SET @sql = CONCAT('SELECT Action, ', @sql, ' FROM updates GROUP BY Action');
PREPARE stmt FROM @sql;
EXECUTE stmt;
The actual Question
I am not able to work out how to execute this using PHP so that I can display this output on a webpage. Is anyone able to either provide me with the PHP code to perform this or point me in the right direction of information required.
I have read a number of articles but I think the issue is that I don't know what I'm actually looking for. At first I assumed it was how to run prepared statements within PHP but this didn't appear to help.
The answer is simple. You can always run every query from your set as a separate query() call. this is universal rule for running multi-query statements.
but please remember that by this kind of query your are essentially running an SQL injection against your own database. As long as the field type is
date
it cannot do any harm, but for any text type - watch out!This a bit of code how to access to a database with MySQLI if this could help you
Assuming you are using mysqli (and not PDO) you can't use a simple query() because you want to execute multiple commands. You will need to use multi_query() in combination with store_result(), more_results() and next_result().
Here is some code I used once: