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

时间:2013-11-04 22:07:54

标签: php mysql sql pdo foreach

需要在foreach中运行2个查询,但不能没有错误。

所以,我有这个显示评论:

$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; ?>

然后,我想在用户数据库上运行另一个查询以检查用户拥有的权限,然后将注释用户名的类设置为该类。

该查询将是例如。

    <?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; ?>

如何正确地解决这个问题?

P.S。据我所知,上面的代码可能会让人哭泣。

2 个答案:

答案 0 :(得分:4)

对连接使用单个查询。此外,由于您正在使用PDO,因此您应该使用参数化查询而不是连接字符串。

$query = "SELECT * FROM comments c
          JOIN users u ON u.id = c.userID
          WHERE updatepostid = :updatepostid";
try {
    $stmt = $db->prepare($query);
    $stmt->execute(array(':updatepostid' => $postID));
    }
catch (PDOException $ex) 
    {
        die("Failed to run query: " . $ex->getMessage());
    }

答案 1 :(得分:-2)

您可以按照提及的那样加入表:

<?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;

?>

您还可以在第一个循环中插入第二个循环,并为第二个循环分配单独的变量,以便它不会与第一个循环冲突,如下所示,但加入将是更好的选择:

<?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;

?>