dtf24224 2017-12-05 17:26
浏览 78
已采纳

我如何在foreach中爆炸Group_Concat

Query MySQL here query to get the result for all users with Group_concat

$result = $con->prepare("SELECT
    results.StuID,
    subjects_d.SubjectID,
    exams.ID,
    GROUP_CONCAT(results.ExamID,',',results.Exam1,',',results.Exam2,',',results.Exam3 ORDER BY results.ExamID  ASC) AS data
FROM
    results
LEFT JOIN
    users
ON 
    users.UID = results.StuID
LEFT JOIN
    subjects_d
ON
    subjects_d.SubID = results.Sub_ID
LEFT JOIN
    exams
ON 
    exams.ID = results.ExamID
WHERE 
    subjects_d.C_ID = 3
AND 
    subjects_d.SubjectID = 4
GROUP BY results.StuID");
$result->execute(array($classid, $subjectid));
$results = $result->fetchAll();

This is Result from mysql

PHP code

<?php foreach($results as $result) { ?>
    <?php echo '<pre>';print_r($result)?>
<?php } ?>

print_r

Array
(
    [StuID] => 70
    [0] => 70
    [SubjectID] => 4
    [1] => 4
    [ID] => 1
    [2] => 1
    [data] => 1,0,0,0,2,0,0,0,3,0,0,0,4,0,0,0
    [3] => 1,0,0,0,2,0,0,0,3,0,0,0,4,0,0,0
)

Now how I can explode this data array in foreach loop Thank you

  • 写回答

1条回答 默认 最新

  • dongyong1400 2017-12-05 17:33
    关注

    Explode data string with comma as delimiter in foreach loop.

    foreach($results as $result) {
        // explode with comma 
        $data_array = explode(',',$result['data']);
     } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:我知道这个问题对你们来说肯定so easy
  • ¥15 wpf datagrid如何实现多层表头
  • ¥15 为啥画版图在Run DRC会出现Connect Error?可我Calibre的hostname和计算机的hostname已经设置成一样的了。
  • ¥20 网站后台使用极速模式非常的卡
  • ¥20 Keil uVision5创建project没反应
  • ¥15 mmseqs内存报错
  • ¥15 vika文档如何与obsidian同步
  • ¥15 华为手机相册里面的照片能够替换成自己想要的照片吗?
  • ¥15 陆空双模式无人机飞控设置
  • ¥15 sentaurus lithography