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

在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.

图片转代码服务由CSDN问答提供 功能建议

需要在foreach中运行2个查询,但不能没有错误。 < p>所以,我有这个用于显示注释:

  $ query ='SELECT * FROM comments WHERE updatepostid =“'。$ postID。'”'; 
 try {  
 $ stmt = $ db-&gt; prepare($ query); 
 $ stmt-&gt; execute(); 
 $ countcomments = $ stmt-&gt; rowCount(); 
} 
 catch(PDOException $  ex)
 {
 die(“无法运行查询:”。$ ex-&gt; getMessage()); 
} 
 
 $ rows = $ stmt-&gt; fetchAll(); 
foreach($ 行为$ row):
 $ commentID = $ row ['commentID']; 
 $ usercommentID = $ row ['userID']; 
 $ commentusername = ucfirst($ row ['commentusername']); 
  $ comment = ucfirst($ row ['comment']); 
 $ updatepostid = $ row ['updatepostid']; 
 
&lt; div class =“textcomment”&gt; 
&lt;?php 
 echo“  &lt; a class ='$ rightscommentcolor'&gt; $ commentusername:&lt; / a&gt;&amp; nbsp; $ comment“; 
?&gt; 
&lt; / div&gt; 
 
 
&lt;?php endforeach  ;  ?&gt; 
   
 
 

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

该查询将是例如

 &lt;?php 
 
 $ query2 ='SELECT * FROM users WHERE id  =“'。$ usercommentID。'”'; 
尝试{
 $ stmt = $ db-&gt; prepare($ query2); 
 $ stmt-&gt; execute(); 
} 
 catch(PDOException)  $ ex)
 {
 die(“无法运行查询:”。$ ex-&gt; getMessage()); 
} 
 
 $ rows = $ stmt-&gt; fetchAll(); 
foreach(  $ rows为$ row):
 $ rights = $ row ['rights']; 
 
if($ rights ='1'){
 $ rightscommentcolor ='userrights1'; 
} else if($ rights)  ='5'){
 $ rightscommentcolor ='userrights5'; 
} 
 
?&gt; 
&lt;?php endforeach;  ?&gt; 
   
 
 

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

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

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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;
    
    ?>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongshi3818 2013-11-04 22:19

    Use a single query with a join. Also, since you're using PDO, you should use parametrized queries rather than concatenating strings.

    $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());
        }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题