在while循环下使用while时,sql查询未运行

时间:2012-01-16 07:04:47

标签: php mysql while-loop

我试图在while循环中运行另一个while循环,但只有一个循环正在运行,即使我尝试通过alert回显整个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部分未运行[下面的代码]。当我尝试回显时,它显示为空,至少应该打印出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'];

                }         

1 个答案:

答案 0 :(得分:-1)

在第一个while循环之前使用以下查询,

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