2013-11-04 48 views
0

需要在foreach中運行2個查詢,但不能沒有錯誤地執行。在foreach循環中運行第二個查詢?

所以,我有這個用於顯示評論:

$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.我明白上面的代碼可能會讓人哭泣。

+1

在你的第一個SQL語句,你需要'JOIN'了'comments'和'users'表,所以你只需要一次查詢數據庫:HT tp://dev.mysql.com/doc/refman/5.5/en/join.html – jeroen

+0

你會得到什麼錯誤? – Barmar

回答

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

?> 
+0

現在使用最後一個,因爲它的工作原理:) 如果可能的話,試着改變它一個JOIN語句:) – Lovelock

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()); 
    } 
+0

已經嘗試過使用這個,但是在沒有錯誤之前得到相同的問題,但似乎在殺死頁面。我的錯誤處理程序似乎沒有工作,並且不顯示任何錯誤,當刪除die()頁面的其餘部分加載。 我也改變了query2查詢但沒有改變。 – Lovelock

+0

我不明白那可能是怎麼回事。如果它進入'catch'模塊,那麼它應該在消息死亡時顯示消息。 – Barmar

+0

我在回答中有一個拼寫錯誤,'$ query2'而不是'$ query'。如果您複製它,請嘗試修復它。 – Barmar