mysql到html显示相同值列的值

5w9g7ksd  于 2021-06-21  发布在  Mysql
关注(0)|答案(3)|浏览(419)

我很难得到正确的输出。我有一张这样的table:

我使用以下php代码创建表:

<?php 
include_once("includes/dbh.inc.php");
if(isset($_POST["r_id"])){
    $output = '';

    $query = "SELECT * FROM relatorioc WHERE datas = '".$_POST["r_id"]."'";
    $res = mysqli_query($conn, $query);

    $output .='
    <div class="table-responsive">
        <table class="table table-bordered">';

        while($row = mysqli_fetch_array($res)){                
            $output .= '
                `<tr>
                    <td>'.$row['linha'].'</td>
                    <td>'.$row['user'].'</td>
                    <td>'.$row['prob'].'</td>
                    <td>'.$row['res'].'</td>
                    <td>'.$row['timep'].'</td>
                    <td>'.$row['timei'].'</td>
                </tr>';
        }
        $output .= "</table></div>";
        echo $output;    
}
xoefb8l8

xoefb8l81#

你需要先做‘groupby’查询,然后再做其它带有col分组的查询,如果没有你对很多mysql的经验,也可以做其它更复杂的查询。
(在第一个查询中按“linha”分组,在其他查询之前按where分组(第一个结果为“linha”值)
选择from relatorioc data='“.”$\u post[“r\u id”]。“'”;按前面的“linha”分组:$query=“selectfrom relatorioc where datas='”..$\u post[“r\u id”]。“'”和linha='上一个结果';
这对你来说是个简单的解决办法。

qqrboqgw

qqrboqgw2#

首先根据您的需要对结果进行分组查询->:

select * from relatorioc datas = '". $ _ POST [" r_id "]."' "; group by 'linha' with it will provide the necessary data to the following query ->: $ query = "SELECT * FROM relatoryc WHERE datas = '". $ _ POST ["r_id"]. "'" and linha = 'previous result';
j8yoct9x

j8yoct9x3#

在我回答您的问题之前,您必须了解代码中的aobvious安全漏洞,这是一个sql注入:在进入sql查询之前,您不会转义输入( $_POST["r_id"] )
对于您的问题,我所做的只是创建一个数组,然后在while循环中输入按“linha”分组的行,然后使用两个 foreach . 我还在sql查询中为您转义了输入( mysqli_real_escape_string )

if (isset($_POST["r_id"])) {
    $output = '';

    $query = "SELECT * FROM relatorioc WHERE datas = '" . mysqli_real_escape_string($conn, $_POST["r_id"]) . "'";
    $res = mysqli_query($conn, $query);

    $data = [];

    while ($row = mysqli_fetch_array($res)) {
        $data[$row['linha']][] = $row;
    }

    $output .= '';

    foreach ($data as $linha => $items) {
        $output .= '<p>' . $linha . '</p>';
        $output .= '
        <div class="table-responsive">
            <table class="table table-bordered">';

        foreach ($items as $row) {
            $output .= '
            <tr>
                <td>' . $row['linha'] . '</td>
                <td>' . $row['user'] . '</td>
                <td>' . $row['prob'] . '</td>
                <td>' . $row['res'] . '</td>
                <td>' . $row['timep'] . '</td>
                <td>' . $row['timei'] . '</td>
            </tr>';
        }

        $output .= '</table></div>';
    }

    echo $output;
}

相关问题