0

我对 PhP 还是很陌生,我在这里有这样的声明:

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'GROUP_CONCAT(CASE WHEN `Date` = ''',
      `Date`,
      ''' THEN hours ELSE NULL END) AS `',
      `Date`, '`'
    )
  ) INTO @sql
FROM Days;

SET @sql = CONCAT('SELECT Name, ', @sql,'
                     FROM Days
                    GROUP BY Name
                  ');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

我正在尝试用 Php 执行它,所以我有与这个小提琴相同的 html 表:

http://sqlfiddle.com/#!2/37156/8

4

1 回答 1

0

When I use prepare statements like this I return fetchAll from the function that the sql is in. That returns an array of all the data.

I can then format it in the HTML page as I see fit by:

$data = nameOfYourFunctionThatReturnsFetchAll;

<?php echo '<li>' . $data['nameOfFieldFromDatabase'] . </li> ?>

Etc etc

EDIT:

<?php 

function getData(){

sql here
try{
execute sql here
}catch PDOException($e){
$e->getMessage();
}
return fetchAll();
}

then

$data = getData();

then in your HTML

<?php echo '<li>' . $data['name'] . </li> ?>

You need to know that values within the $data['name'] refer to the names of the field in your database so make sure they match

于 2013-07-25T13:01:05.053 回答