php - sql how i get username from seesion id? -


now have doing chatroom message username getting seesion username. log in name username same you. how getting username user database session_user id?

my table is

  • user - have id,username
  • chatroom have id,name
  • chatroom_chat have chatroom_id whihc relationship chatroom,chat_id relationship chat
  • chat have id,user_id,chat.date

how user_id chat , username user table?

here code php , sql

    $chatroomid=$_get['chatroomid']; $userid = $_session['id'];  $sql="select * chatroom_chat"; $result1 = mysqli_query($connection, $sql) or die(mysqli_error($connection));       while ($row = mysqli_fetch_array($result1)) {     $chat = $row['chat_id'];     $getchatdata = mysqli_query($connection,"select * (         select * chat id = '$chat' order id desc limit 0,40         ) sub         order id asc ") or die(mysqli_error($connection));          while($row3 = mysqli_fetch_array($getchatdata)) {             $username = $_session['username'];             $color = ($row3['user_id'] == $userid) ? '#ffffff' : '#66ffff';             $position = ($row3['user_id'] == $userid ) ? 'right' : 'left';             $border = ($row3['user_id'] == $userid) ? ' 1px solid black ' : ' none ';              echo "<div class='msg-dateandtime' style='text-align:$position; float:$position;'> <div class='left-username' style='color:blue;'>" .$username."</div>"                     . "<div class='space'></div>"                     . "<div class='right-date'>  ". $row3['date'] ." </div></div>"                     . "<div class='wrap-message' style='background-color:$color; border:$border; float:$position;'>"                     . "<p style 'text-align=$position; margin:0; padding:0; text-align:left;'> ".$row3['chat']."</p></div>";         }  } 

from post, assuming question: "how getting username user database session_user id?"

you showed user table content follows:

      user    __________________   |  id  |  username |   |------|-----------|   |   1  |    jack   |   |   2  |    sam    |   |   3  |    ana    | 

so if session_user in question equivalent id id in user table, use following code:

$sql = "select username username id = {$_session['session_user']}"; 

the previous code assuming session_user key value in $_session super global. however, if session_user actual php variable in code ($session_user), replace $_session['session_user'] $session_user in previous sql statement.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -