How do I produce a dynamic MySQL pivot table with PHP?

后端 未结 3 781
情书的邮戳
情书的邮戳 2021-01-03 15:34

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

3条回答
  •  粉色の甜心
    2021-01-03 16:04

    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.

    $mysqli->query('SET @sql = NULL');
    $mysqli->query(<<query("SET @sql = CONCAT('SELECT Action, ', @sql, ' FROM updates GROUP BY Action');");
    $mysqli->query("PREPARE stmt FROM @sql");
    
    $res = $mysqli->query("EXECUTE stmt");
    var_dump($res->fetch_all(MYSQLI_ASSOC));
    

    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!

提交回复
热议问题