2012-01-16 96 views
0

我试图在while循环中运行另一个while循环,但只有一个循环正在运行,即使当我尝试通过alert来回显整个sql语句时,他也没有给出结果。任何人都可以帮忙吗?在while循环下使用时,sql查询不运行

       //Insert Comments 
        public function Insert_Comment($uid,$msg_id,$comment,$ip,$cfid) 
        { 
        $comment=mysql_real_escape_string($comment); 

          $time=time(); 
         $ip=$_SERVER['REMOTE_ADDR']; 

         $query = mysql_query("SELECT com_id,comment FROM `comments` WHERE uid_fk='$uid' and msg_id_fk='$msg_id' order by com_id desc limit 1 ") or die(mysql_error()); 
         $result = mysql_fetch_array($query); 
         if ($comment!=$result['comment']) { 
         $query = mysql_query("INSERT INTO `comments` (comment, uid_fk,msg_id_fk,ip,created,cfid) VALUES (N'$comment', '$uid','$msg_id', '$ip','$time','$cfid')") or die(mysql_error()); 


       $sql1=mysql_query("select * from users_profile where uid='$uid'"); 
       while($row1=mysql_fetch_array($sql1)) 
        { 
        $reqfname=$row1['fname']; 
        $reqpic=$row1['profile_pic']; 
        $reqsex=$row1['sex']; 
        $requid=$row1['uid']; 
        $notify = mysql_query("insert into notification(notification_id,type_id,item_id,user_id,owner_user_id,is_seen,time_stamp,title_html,title_text,body_html,body_text,href,icon_url)values('','wall_comments','4','$cfid','$uid','0',now(),'<b> Global Notification</b>','Global Notification','<img alt=notification icon src=$reqpic></span><span class=message>$reqfname shared view on your Status Update</span><span class=close></span>','$reqfname shared view on your Status Update','demo','')"); 
       $dim=mysql_query("SELECT m.msg_id,m.uid_fk,c.com_id,c.msg_id_fk,c.uid_fk,c.cfid,n.type_id,n.user_id,n.owner_user_id,n.href,n.time_stamp from messages m ,comments c, notification n where m.msg_id=c.msg_id_fk and m.uid_fk=c.uid_fk and c.uid_fk=n.owner_user_id and c.cfid=n.user_id and n.type_id='wall_comments' and c.uid_fk=$uid and c.cfid=$cfid and n.href='$href' order by n.time_stamp desc"); 
       while($rom=mysql_fetch_array($dim)) 
        { 
        echo "SELECT m.msg_id,m.uid_fk,c.com_id,c.msg_id_fk,c.uid_fk,c.cfid,n.type_id,n.user_id,n.owner_user_id,n.href,n.time_stamp from messages m ,comments c, notification n where m.msg_id=c.msg_id_fk and m.uid_fk=c.uid_fk and c.uid_fk=n.owner_user_id and c.cfid=n.user_id and n.type_id='wall_comments' and c.uid_fk=$uid and c.cfid=$cfid and n.href='$href' order by n.time_stamp desc"; 
       echo "man"; 
        $oldhref=$rom['href']; 

       }   
          $newquery = mysql_query("SELECT C.com_id, C.uid_fk, C.comment, C.msg_id_fk, C.created, U.username FROM comments C, users U where C.uid_fk=U.uid and C.uid_fk='$uid' and C.msg_id_fk='$msg_id' order by C.com_id desc limit 1 "); 
          $result = mysql_fetch_array($newquery); 

          return $result; 

          } 
         } 
         else 
         { 
         return false; 
         } 

        } 

在整个查询中,$ dim部分未运行[code below]。当我尝试回显这个时,它显示为空,至少应该打印sql查询。

   $dim=mysql_query("SELECT m.msg_id,m.uid_fk,c.com_id,c.msg_id_fk,c.uid_fk,c.cfid,n.type_id,n.user_id,n.owner_user_id,n.href,n.time_stamp from messages m ,comments c, notification n where m.msg_id=c.msg_id_fk and m.uid_fk=c.uid_fk and c.uid_fk=n.owner_user_id and c.cfid=n.user_id and n.type_id='wall_comments' and c.uid_fk=$uid and c.cfid=$cfid and n.href='$href' order by n.time_stamp desc"); 
       while($rom=mysql_fetch_array($dim)) 
        { 
        echo "SELECT m.msg_id,m.uid_fk,c.com_id,c.msg_id_fk,c.uid_fk,c.cfid,n.type_id,n.user_id,n.owner_user_id,n.href,n.time_stamp from messages m ,comments c, notification n where m.msg_id=c.msg_id_fk and m.uid_fk=c.uid_fk and c.uid_fk=n.owner_user_id and c.cfid=n.user_id and n.type_id='wall_comments' and c.uid_fk=$uid and c.cfid=$cfid and n.href='$href' order by n.time_stamp desc"; 
       echo "man"; 
        $oldhref=$rom['href']; 

       }   
+0

是uid是一个整数? – 2012-01-16 07:32:41

+2

请改善你的代码缩进,让你的代码看起来更悦目,让别人更容易帮助你。这里有一个格式指南:'缩进代码4个空格',替换''标签''替换'4空格' – ianace 2012-01-16 08:12:47

+0

soory为麻烦的朋友,感谢您查看代码,很高兴在我自己后面找到了解决方案。谢谢 – 2012-01-16 10:17:49

回答

-1

只需使用下面的查询您的第一个while循环之前,

$sql1=mysql_query("select * from users_profile where uid=$uid"); 
+0

只要'$ uid'是一个整数就不会有任何区别。 – Eric 2012-01-16 07:20:15