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

喜夏-厌秋 提交于 2019-12-23 09:49:29

问题


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.


回答1:


Use an alias for the column.

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




回答2:


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


来源:https://stackoverflow.com/questions/1154278/using-php-to-return-group-concatcolumn-x-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!