0

I am a total beginner in PHP and MySQL. I am trying to make a chat application where users can open their chat thread and get the messages displayed. I am trying to query messages for the chat message and echo the message in the proper div block. Is it possible to make an If statement that checks if the $_SESSION['uid'] was true for a column, then output the message in it? Then echo it for each row? Sorry in advance, I am really new so I dont know all of the proper procedures and SQL commands. Thanks!

session_start();
$stmt = $conn->prepare("
     SELECT message from messages WHERE conv_id=?
     AND (user_send=? OR user_receive=?)
     ORDER BY timestamp ASC
                       ");
$stmt->bind_param('iii', $_POST['conv_id'], $_SESSION['uid'],$_SESSION['uid']);
$stmt->execute();
$stmt->bind_result($message);
$message_right = '';
$message_left = '';
     while($stmt->fetch()){
        if (user_send = $_SESSION['uid']){
$message_right .= '<div class="display-on-right">'.$message.'</div>';
echo $message_right;
          }elseif(user_receive = $_SESSION['uid']){
$message_left .= '<div class="display-on-left">.$message.</div>';
echo $message_left;
          }
}
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • What results are you getting now as opposed to the desired results? Enable error reporting and `mysqli_error($conn)` for possible errors. – Funk Forty Niner Mar 24 '20 at 04:28
  • not getting any results, i am kind of new. I was binding SESSION twice for each question mark. I know I cant explicitly say that "if column = value. I just dont know how to do this yet –  Mar 24 '20 at 04:30
  • What's `user_send`? – Rotimi Mar 24 '20 at 04:31
  • the column name in the messages row, as is user_receive, i am trying to see if that column name equals the session value for the row –  Mar 24 '20 at 04:32
  • " I dont know all of the proper procedures" is not actually an excuse. You are supposed to learn using some tutorial or a book. Such a basic stuff is covered in any tutorial. That said, your code is much better than average. – Your Common Sense Mar 24 '20 at 04:40
  • i dont see how that questions comes close to answering my question here. I am already using `while($stmt->fetch()){` which was suggested. The asker of that question didnt even know how to do that. My question is much more specific. I am asking how to deal with a CASE scenario inside of the `while($stmt->fetch()){` based on the column name equaling the session value for each queried result, then outputting a result based upon each. How is that even remotely related? –  Mar 24 '20 at 04:57
  • you, actually, perfectly know how to deal with a CASE scenario, which can be clearly seen from your code. the only problem is that you don't know how to address variables fetched. – Your Common Sense Mar 24 '20 at 05:11

1 Answers1

-2

What you do is simple.

Add the columns you want to your query and then you'll be able to access it after getting the results from the query.

$stmt = $conn->prepare("
     SELECT message, user_send, user_receive from messages WHERE conv_id=?
     AND (user_send=? OR user_receive=?)
     ORDER BY timestamp ASC");

$stmt->bind_result($message, $user_send, $user_receive);

if ($user_send == $_SESSION['uid']){ //single equals to is for assigning. double is for comparing
   //do something
}
Rotimi
  • 4,783
  • 4
  • 18
  • 27
  • yes, but i need them to be ordered correctly based upon the order they were added to the database. as well as be displayed on the right for the current user who sent, and the left for the current user who received –  Mar 24 '20 at 04:36