Reputation: 2420
Need some help in php mysql suppose i am logged in as $active_username and I want to see all the messages between me($active_username) and my friend($username) from a table pvt_message, where i have columns(pid,user_from,user_to,body). I tried using below code:
$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 are the code to show data fetched on table, there is no error in that,so i didnt post. this above code fetched me only those message i had sent to my friend.
again i used below code instead of above one:
$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";
}
n now it fetched me all those message he/she sent me.
I want to view message we both sent each other, in asending order of the primary key of the table. so i tried using:
$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";
}
how to get those data in table??? where user_from can be both me($active_username) or he($username) and user_to can be both me($active_username) or he ($username). Thankyou in advance :)
Upvotes: 1
Views: 85
Reputation: 111
Put the below code before fetching from the user table and pass the $qry
variable in the mysql_query
function.
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);
Upvotes: 1
Reputation: 2420
Thankyou everyone, for suggestion. I had to change a little bit, to get result I wanted.
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>
Upvotes: 0
Reputation: 111
just change your query like below
$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());
Upvotes: 1
Reputation: 909
Try it
SELECT * FROM pvt_messages WHERE user_from IN ('$active_username', '$username') AND user_to IN ('$active_username', '$username')
It will work if not have messages in DB from active_username
to active_username
Upvotes: 1
Reputation: 2420
I was able to get messages sequentially between my friend and me.... on basis of pid of the table pvt message using
$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 />
";
............. but now while i am fetching the user datas , to show name of user and profile pic too, only my pic is beling displayed in both my messages and my friend messages. there is something wrong in line:
$get_user_info = mysql_query("SELECT * FROM users WHERE (username='$active_username' || username='$username')");
Upvotes: 0