如何在单个mysql_query中检查2个变量

时间:2014-05-17 11:47:58

标签: php mysql

在php mysql中需要一些帮助 假设我以$ active_username身份登录,我想从表pvt_message中查看我($ active_username)和我的朋友($ username)之间的所有消息,其中我有列(pid,user_from,user_to,body)。 我尝试使用以下代码:

$getmessages= mysql_query("SELECT * FROM pvt_messages WHERE user_from='$active_username' && user_to='$username' ORDER BY id ASC ") or die(mysql_error());

                        while ($row = mysql_fetch_assoc($getmessages )) 
                        {
                            $body = $row['msg_body'];   
                            $date_send = $row['date'];
                            $opened = $row['opened'];
                            $get_user_info = mysql_query("SELECT * FROM users WHERE username='$active_username'");
                            $get_info = mysql_fetch_assoc($get_user_info);
                            $profilepic_info = $get_info['profile_pic'];
                            if ($profilepic_info == "") {
                             $profilepic_info = "./images/default_pic.jpg";
                            }

rest是显示在表上获取的数据的代码,没有错误,所以我没有发布。 上面的代码只提取了我发给我朋友的那些消息。

我再次使用下面的代码而不是上面的代码:

$getmessages= mysql_query("SELECT * FROM pvt_messages WHERE user_from='$active_username' && user_to='$username' ORDER BY id ASC ") or die(mysql_error());

                        while ($row = mysql_fetch_assoc($getmessages )) 
                        {
                            $body = $row['msg_body'];   
                            $date_send = $row['date'];
                            $opened = $row['opened'];
                            $get_user_info = mysql_query("SELECT * FROM users WHERE username='$active_username'");
                            $get_info = mysql_fetch_assoc($get_user_info);
                            $profilepic_info = $get_info['profile_pic'];
                            if ($profilepic_info == "") {
                             $profilepic_info = "./images/default_pic.jpg";
                            }
现在它把他/她发给我的所有信息都拿给了我。

我希望按照表的主键的顺序查看我们彼此发送的消息。 所以我尝试使用:

$getmessages= mysql_query("SELECT * FROM pvt_messages WHERE (user_from='$active_username' || user_from='$username') && (user_to='$username' || user_to='$active_username') ORDER BY id ASC ") or die(mysql_error());

                        while ($row = mysql_fetch_assoc($getmessages )) 
                        {
                            $body = $row['msg_body'];   
                            $date_send = $row['date'];
                            $opened = $row['opened'];
                            $get_user_info = mysql_query("SELECT * FROM users WHERE username='$active_username' || username ='$username'");
                            $get_info = mysql_fetch_assoc($get_user_info);
                            $profilepic_info = $get_info['profile_pic'];
                            if ($profilepic_info == "") {
                             $profilepic_info = "./images/default_pic.jpg";
                            }

如何在表格中获取这些数据? user_from可以是me($ active_username)或者他($ username),user_to可以是me($ active_username)或者是($ username)。 谢谢你提前:))

5 个答案:

答案 0 :(得分:1)

试试吧

SELECT * FROM pvt_messages WHERE user_from IN ('$active_username', '$username') AND user_to IN ('$active_username', '$username')

如果在active_usernameactive_username

的数据库中没有消息,它将会有效

答案 1 :(得分:1)

只需更改您的查询,如下所示

$getmessages= mysql_query("SELECT * FROM pvt_messages WHERE ((user_from='$active_username' && user_to='$username') || (user_from='$username' && user_to='$active_username')) ORDER BY id ASC ") or die(mysql_error());

答案 2 :(得分:1)

在从用户表中提取之前输入以下代码,并在$qry函数中传递mysql_query变量。

if($['user_from'] == $active_username)
{
    $qry = "SELECT * FROM users WHERE username ='$active_username'";
}
else
{
    $qry ="SELECT * FROM users WHERE username ='$username'";
}

$get_user_info = mysql_query($qry);

答案 3 :(得分:0)

我能够在朋友和我之间顺序收到消息....基于pvt表pvt消息的pid使用

$getmessages= mysql_query("SELECT * FROM pvt_messages WHERE (user_from='$active_username' && user_to='$username') || (user_from='$username' && user_to='$active_username')  ORDER BY id ASC ") or die(mysql_error());

                        while ($row = mysql_fetch_assoc($getmessages )) 
                        {
                            $body = $row['msg_body'];   
                            $date_send = $row['date'];
                            $opened = $row['opened'];
                            $get_user_info = mysql_query("SELECT * FROM users WHERE (username='$active_username' || username='$username')");
                            $get_info = mysql_fetch_assoc($get_user_info);
                            $profilepic_info = $get_info['profile_pic'];
                            if ($profilepic_info == "") {
                             $profilepic_info = "./images/default_pic.jpg";
                            }
                            else
                            {
                             $profilepic_info = "./userdata/profile_pics/".$profilepic_info;
                            }
                            echo "
                                    <div style='float: left;padding-top:5px;'>
                                            <img src='$profilepic_info' height='20' width='20'>
                                    </div>
                                    <div style='padding-left:20px;padding-top:5px;'>
                                        <a href='$active_username'>Me:</a> $body
                                    </div></br>
                                    <div  style=' margin-left:80px;'>
                                     <br />
                                    </div>
                                    <hr />
                            ";

.............但是现在当我拿取用户数据时,为了显示用户和个人资料图片的名称,只有我的图片显示在我的消息和我的朋友消息中。 排队有问题:

$get_user_info = mysql_query("SELECT * FROM users WHERE (username='$active_username' || username='$username')");

答案 4 :(得分:0)

谢谢大家,感谢您的建议。我不得不改变一点,得到我想要的结果。

while ($row = mysql_fetch_assoc($getmessages )) 
                        {
                            $body = $row['msg_body'];   
                            $date_send = $row['date'];
                            $opened = $row['opened'];
                            $whosent= $row['user_from'];
                            $whoreceived= $row['user_to'];
                            $seen=$row['opened'];

                            /////////////////////////////////////////////////////
                            $get_user_info = mysql_query("SELECT * FROM users WHERE (username='$whosent')");
                            ////////////////////////////////////////////////////
                            $get_info = mysql_fetch_assoc($get_user_info);
                            $profilepic_info = $get_info['profile_pic'];
                            if ($profilepic_info == "") {
                             $profilepic_info = "./images/default_pic.jpg";
                            }
                            else
                            {
                             $profilepic_info = "./userdata/profile_pics/".$profilepic_info;
                            }
                            echo "
                                    <div style='float: left;padding-top:5px; padding-left:20px;'>
                                            <img src='$profilepic_info' height='50' width='40'>
                                    </div>
                                    <div style='margin-left:50px;padding-top:5px;'>
                                        <a href='$active_username' style='color:black'> $whosent</a> $body
                                    </div></br>
                                    <div  style=' margin-left:30px;'>
                                    <font size='2px'>sent on: $date_send</font>