Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using php to return GROUP_CONCAT('column x') values

I am trying to use PHP to return SQL values into an HTML table. I am able to get every column to populate without a problem except for the last column, "GROUP _ CONCAT (provision_id)."

Relevant code:

<?php

global $wpdb;
$wpdb->show_errors();
$contents = $wpdb->get_results( $wpdb->prepare("SELECT salaries.id, name, remaining, contract_value, GROUP_CONCAT( provision_id ) FROM salaries LEFT JOIN contracts ON contracts.id = salaries.id GROUP BY salaries.id"));

 ?>

   [table header stuff...]

<?php 

    foreach ($contents as $content) {
        ?>   
             <tr>
                    <td><?php echo $content->name ?></td>
                    <td><?php echo $content->remaining ?></td>
                    <td><?php echo $content->contract_value ?></td>
                    <td><?php echo $content->GROUP_CONCAT(provision_id) ?></td>

    <?php }; ?>   

            </tr>

Just echoing $content->provision-id doesn't work either.

like image 234
Adam Avatar asked Jul 20 '09 15:07

Adam


2 Answers

If you are fetching into objects, you should give your columns names that are legal class member identifiers in PHP (I'll link to the manual, although their description of valid variable names is horrible):

SELECT ... GROUP_CONCAT(provision_id) AS provisions
like image 24
soulmerge Avatar answered Oct 03 '22 22:10

soulmerge


Use an alias for the column.

GROUP_CONCAT( provision_id ) as pids
...
echo $content->pids

like image 99
VolkerK Avatar answered Oct 03 '22 20:10

VolkerK