0

I'm still very new in PhP, and I have this statement here:

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;

and I'm trying to execute it with Php so I have the same html table like in this fiddle:

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Iznogud
  • 73
  • 6

1 Answers1

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

null
  • 3,469
  • 7
  • 41
  • 90
  • Ok, I put the entire statment in $data variable, and the executed: ' . $data['name'] . ?> to test it, I I just got one letter like result. Is this what I should be doing? – Iznogud Jul 25 '13 at 13:20
  • "the entire statement into a variable" ?! Put it in a function :) Then create the variable from the result of that funciton. I'll update answer in a second with an example I use. – null Jul 25 '13 at 13:44