dprq18175 2013-11-04 22:07
浏览 139
已采纳

在foreach循环中运行第二个查询?

Need to run 2 queries inside a foreach but cant do it without errors.

So, i have this for showing comments:

$query = 'SELECT * FROM comments WHERE updatepostid = "' . $postID . '"';
    try {
        $stmt = $db->prepare($query);
        $stmt->execute();
        $countcomments = $stmt->rowCount();
        }
        catch (PDOException $ex) 
        {
            die("Failed to run query: " . $ex->getMessage());
        }

$rows = $stmt->fetchAll();
foreach ($rows as $row):
$commentID       = $row['commentID'];
$usercommentID   = $row['userID'];
$commentusername = ucfirst($row['commentusername']);
$comment         = ucfirst($row['comment']);
$updatepostid    = $row['updatepostid'];

<div class="textcomment">
    <?php
        echo "<a class='$rightscommentcolor'>$commentusername:</a>&nbsp; $comment";
    ?>
</div>


<?php endforeach; ?>

I then however want to run another query on the users database to check what rights the user has and then set the class of the comments username to that class.

That query would be e.g.

    <?php

$query2 = 'SELECT * FROM users WHERE id = "' . $usercommentID . '"';
    try {
        $stmt = $db->prepare($query2);
        $stmt->execute();
        }
        catch (PDOException $ex) 
        {
            die("Failed to run query: " . $ex->getMessage());
        }

$rows = $stmt->fetchAll();
foreach ($rows as $row):
$rights = $row['rights'];

if ($rights = '1') {
    $rightscommentcolor = 'userrights1';
} else if ($rights = '5') {
    $rightscommentcolor = 'userrights5';
}

?>
<?php endforeach; ?>

How is the proper way to go about this?

P.S. i understand that the above code will probably make people cry.

  • 写回答

2条回答 默认 最新

  • doubaran2438 2013-11-04 22:22
    关注

    You can join the tables in select as mentioned:

    <?php
    
    $query = "SELECT * FROM comments c
              JOIN users u ON u.usercommentID = c.userID
              WHERE updatepostid = :updatepostid";
        try {
            $stmt = $db->prepare($query);
            $stmt->execute();
            $countcomments = $stmt->rowCount();
            }
            catch (PDOException $ex) 
            {
                die("Failed to run query: " . $ex->getMessage());
            }
    
    $rows = $stmt->fetchAll();
    foreach ($rows as $row):
    
    $commentID       = $row['commentID'];
    $usercommentID   = $row['userID'];
    $commentusername = ucfirst($row['commentusername']);
    $comment         = ucfirst($row['comment']);
    $updatepostid    = $row['updatepostid'];
    
    if ($rights = '1') {
        $rightscommentcolor = 'userrights1';
    } else if ($rights = '5') {
        $rightscommentcolor = 'userrights5';
    }
    
    echo "<div class="textcomment"><a class='$rightscommentcolor'>$commentusername:</a>&nbsp; $comment</div>";
    
    endforeach;
    
    ?>
    

    You also could insert the second loop within the first and assign separate variables to that second loop so that it does not conflict with the first as below, but joining would be a better option:

    <?php
    
    $query = 'SELECT * FROM comments WHERE updatepostid = "' . $postID . '"';
        try {
            $stmt = $db->prepare($query);
            $stmt->execute();
            $countcomments = $stmt->rowCount();
            }
            catch (PDOException $ex) 
            {
                die("Failed to run query: " . $ex->getMessage());
            }
    
    $rows = $stmt->fetchAll();
    foreach ($rows as $row):
    
    $commentID       = $row['commentID'];
    $usercommentID   = $row['userID'];
    $commentusername = ucfirst($row['commentusername']);
    $comment         = ucfirst($row['comment']);
    $updatepostid    = $row['updatepostid'];
    
    $query2 = 'SELECT * FROM users WHERE usercommentID = "' . $usercommentID . '"';
        try {
            $stmt2 = $db->prepare($query2);
            $stmt2->execute();
            }
            catch (PDOException $ex2) 
            {
                die("Failed to run query: " . $ex2->getMessage());
            }
    
    $rows2 = $stmt2->fetchAll();
    foreach ($rows2 as $row2):
    $rights = $row2['rights'];
    
    if ($rights = '1') {
        $rightscommentcolor = 'userrights1';
    } else if ($rights = '5') {
        $rightscommentcolor = 'userrights5';
    }
    
    endforeach;
    
    echo "<div class="textcomment"><a class='$rightscommentcolor'>$commentusername:</a>&nbsp; $comment</div>";
    
    endforeach;
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵