数据库的结果相同

时间:2012-11-23 12:32:46

标签: php

我需要在成员个人资料中添加“最后成员主题”,以便显示成员写的最后5个主题。

我使用了这段代码:

$last_topic = mysql_query("SELECT * FROM " .prefix. "TOPICS WHERE T_AUTHOR = '".$ProMemberID."' ORDER BY T_DATE DESC LIMIT 5") or die (mysql_error());
 $result = mysql_num_rows($last_topic);
if ($result > 0){
$rslast = mysql_fetch_array($last_topic);
$topic_id = $rslast['TOPIC_ID'];
$topic_subject = $rslast['T_SUBJECT'];
$topic_mes = $rslast['T_MESSAGE'];
$t_ret = $rslast['T_ENUM'];
$i=0;
   while ($i < $result){

echo '<tr>
  <td colSpan="2" align="center" class="userdetails_data" width="100%">
  <table border="0" width="100%">
   <tr>
   <td>

  <font style="font-size: 9pt" face="Arial"><a href="index.php?mode=t&t='.$topic_id.'">
  '.$topic_subject.'</a> 
    </td>
    <td  class="editedby" width="130">
    <font face="Arial" style="font-size: 9pt"> Edited 
    : 
    '.$t_ret.'</font></td>
   </tr>
  </table>
  </td>
 </tr> 
 ';  



    ++$i;

  }

}else {
echo '<tr><td colSpan="2" align="center" class="userdetails_data" width="100%">
  <span lang="ar-eg">
  <font color="#FF0000" style="font-size: 9pt" face="Arial">This member has no topics yet! </font></span></td></tr>';
}

它有效,但在5行显示相同的主题?? !!

任何解决方案?

2 个答案:

答案 0 :(得分:1)

因为你要分配$ rslast一次,然后在循环中使用这个变量,

试试这个

                $last_topic = mysql_query("SELECT * FROM " .prefix. "TOPICS WHERE T_AUTHOR = '".$ProMemberID."' ORDER BY T_DATE DESC LIMIT 5") or die (mysql_error());
            $result = mysql_num_rows($last_topic);
            if ($result > 0){
                while($rslast = mysql_fetch_array($last_topic)) {


            $topic_id = $rslast['TOPIC_ID'];
            $topic_subject = $rslast['T_SUBJECT'];
            $topic_mes = $rslast['T_MESSAGE'];
            $t_ret = $rslast['T_ENUM'];
            echo '<tr>
              <td colSpan="2" align="center" class="userdetails_data" width="100%">
              <table border="0" width="100%">
               <tr>
               <td>

              <font style="font-size: 9pt" face="Arial"><a href="index.php?mode=t&t='.$topic_id.'">
              '.$topic_subject.'</a> 
                </td>
                <td  class="editedby" width="130">
                <font face="Arial" style="font-size: 9pt"> Edited 
                : 
                '.$t_ret.'</font></td>
               </tr>
              </table>
              </td>
             </tr> 
             ';  


            }

            }else {
            echo '<tr><td colSpan="2" align="center" class="userdetails_data" width="100%">
              <span lang="ar-eg">
              <font color="#FF0000" style="font-size: 9pt" face="Arial">This member has no topics yet! </font></span></td></tr>';
            }

答案 1 :(得分:0)

您获得的值相同,因为$result var的查询总行数(在这种情况下为5),而您将值保存到变量$rslast,而是使用while循环以便循环超过数组值。

$last_topic = mysql_query("SELECT * FROM " .prefix. "TOPICS WHERE T_AUTHOR = '".$ProMemberID."' ORDER BY T_DATE DESC LIMIT 5") or die (mysql_error());
$result = mysql_num_rows($last_topic);

if ($result > 0){
   while($rslast = mysql_fetch_array($last_topic,MYSQL_ASSOC)) {
   //^-----while Loop added to loop through arrays, ^----associative array
     $topic_id = $rslast['TOPIC_ID'];
     $topic_subject = $rslast['T_SUBJECT'];
     $topic_mes = $rslast['T_MESSAGE'];
     $t_ret = $rslast['T_ENUM'];

     echo '<tr>
     <td colSpan="2" align="center" class="userdetails_data" width="100%">
       <table border="0" width="100%">
       <tr>
       <td>

       <font style="font-size: 9pt" face="Arial"><a href="index.php?mode=t&t='.$topic_id.'">
  '.$topic_subject.'</a> 
       </td>
       <td  class="editedby" width="130">
       <font face="Arial" style="font-size: 9pt"> Edited :'.$t_ret.'</font></td>
       </tr>
      </table>
     </td>
   </tr>';        
 }}else {

echo '<tr><td colSpan="2" align="center" class="userdetails_data" width="100%">
  <span lang="ar-eg">
  <font color="#FF0000" style="font-size: 9pt" face="Arial">This member has no topics yet! </font></span></td></tr>';
}