<?php
$query = "SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT('MAX(IF(s.name = \"', `name`,'\", `value`,\"\")) AS ',name)
) INTO @sql
FROM table1;
SET @sql = CONCAT('SELECT s.user_id, ', @sql, '
FROM table1 s
GROUP BY s.user_id
ORDER BY s.user_id');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;";
if ($mysqli->multi_query($query)) {
do {
/* store first result set */
if ($result = $mysqli->store_result()) {
while ($row = $result->fetch_row()) {
printf("%s %s\n", $row[0],$row[1]);
}
$result->free();
}
/* print divider */
if ($mysqli->more_results()) {
printf("-----------------\n");
}
} while ($mysqli->next_result());
}