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

后端 未结 3 761
情书的邮戳
情书的邮戳 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:22

    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:

    $db=mysqli_connect($databasehost,$databaseuser,$databasepass,$databasename) or die ("Connection failed!");
    $result = $db->multi_query($sql);
    
    if ($err=mysqli_error($db)) { echo $err."

    "; } if ($result) { do { if ($res = $db->store_result()) { echo ""; // printing table headers for($i=0; $i{$field->name}"; } echo "\n"; // printing table rows while($row = $res->fetch_row()) { echo ""; foreach($row as $cell) { if ($cell === NULL) { $cell = '(null)'; } echo ""; } echo "\n"; } $res->free(); echo "
    $cell
    "; } } while ($db->more_results() && $db->next_result()); } $db->close();

提交回复
热议问题