有没有办法用数组数据为SQL代码创建foreach

时间:2019-05-29 11:35:54

标签: php mysql foreach

我得到一个查询,该查询获取有关用户的数据。

我需要针对这些用户中的每一个进行第二次查询。

我只为第一个用户获取数据。 (带有第二个查询)

第一个查询:

$query = mysqli_query($conn, "SELECT * FROM tipuser WHERE permissions = '0'");
$ah = mysqli_fetch_array($query);

此外,当我对每个对象($ query为$ x)进行操作时,我会在名字和姓氏中回显所有内容,而当我对$ ah进行相同操作时,则不会显示任何内容。

然后我得到了这些变量:

$firstname = $ah['firstname'];
$lastname = $ah['lastname'];

第二个查询:

$sqlwork = mysqli_query($conn, 
        "SELECT DATE_FORMAT(datum, '%b %Y') AS Monthyear, 
                count(projekt) AS celkem, 
                SUM(projekt = 0) AS tipsport,  
                SUM(projekt = 1) AS slavia, 
                SUM(projekt = 2) AS bet 
        FROM zapasy 
        WHERE komentator1 = '$firstname $lastname' 
           OR komentator2 = '$firstname $lastname' 
        GROUP BY Monthyear");

我认为问题出在数组中的某处,但我找不到它。 感谢您的帮助。

更新: 这是在网站上显示的方法:

         <?php foreach($query AS $usersall) {?>
           <table id="tablePreview" class="table table-hover table-sm table-bordered" style="border-top: 0px solid hsl(0, 0%, 87%);">


    <thead style="color:black; background-color: hsla(0,0%,71%,1.00); border-top: 0px solid hsl(0, 0%, 87%);">
        <tr>
            <th scope="col">Měsíc</th>
            <th scope="col">Zápasů</th>
            <th scope="col">BET</th>
            <th scope="col">TipSport</th>
            <th scope="col">Slavia</th>
            <th scope="col">Brutto</th>
            <th scope="col">Daň</th>
            <th scope="col">Netto</th>
            <th scope="col">Zaplaceno</th>
        </tr>
    </thead>
    <tbody>
        <?php while ($tip = mysqli_fetch_assoc($sqlwork)) {?>
        <tr>
            <td>
                <?=$tip['Monthyear'] ?>
            </td>
            <td>
              <?=$tip['celkem']?>
            </td>
            <td>
                <?=$tip['bet']?>
            </td>
            <td>
                <?=$tip['tipsport']?>
            </td>
            <td>
                <?=$tip['slavia']?>
            </td>
            <td>
                <?php 
                        $betx = $tip['bet']*$bet1cena['cena'];
                        $tipx = $tip['tipsport']*$tipcena['cena']; 
                        $slax = $tip['slavia']*$slaviacena['cena']; 
                        $celkem = $betx+$tipx+$slax;
                        echo "$celkem Kč";
                ?>
            </td>
            <td>
                <?php
                        if($user['fakturuje'] == 0){
                            $dan = $celkem*0.15;
                            echo "$dan Kč";

                        }else{
                            echo "0 Kč";
                        }                                   
                ?>
            </td>
            <td>
                <?php
                   $netto = $celkem-$dan;
                   echo "$netto Kč"; 
                ?>
            </td>
            <td>
                <input type="checkbox">
            </td>
        </tr>

        <?php } ?>
    </tbody>
</table>
<?php }?>

1 个答案:

答案 0 :(得分:0)

尽管您的问题尚不完全清楚,但我认为您从第一个查询中获取了多行,并希望在第一个查询返回的每一行上运行第二个查询

这是一种方法,我很乐意为第二个查询使用准备好的查询,这应该删除所有SQL注入问题以及诸如O'Toole之类的姓氏问题(撇号问题)。这也意味着您只需要编译一次查询,即可根据需要使用新参数运行查询多次。

$q1 = mysqli_query($conn, "SELECT * FROM tipuser WHERE permissions = '0'");

$q2 = "SELECT DATE_FORMAT(datum, '%b %Y') AS Monthyear, 
                count(projekt) AS celkem, 
                SUM(projekt = 0) AS tipsport,  
                SUM(projekt = 1) AS slavia, 
                SUM(projekt = 2) AS bet 
        FROM zapasy 
        WHERE komentator1 = ? 
           OR komentator2 = ? 
        GROUP BY Monthyear");

$stmt2 = $conn->prepare($q2);

while ($ah = mysqli_fetch_array($q1) ) {

    // make the full name field
    $name = $ah['firstname'] . ' ' . $ah['lastname'];

    // bind those values to the prepared query
    $stmt2->bind_param('ss', $name, $name );
    $stmt2->execute();

    // present the results of this query
    // however you like
}