0

I have a private message system and I have this function that returns the IDs of all the users in the conversation (except the sender):

function findOtherUsersInConversation($conversation_id) {
    $sender = findMessageSenderId($conversation_id);
    $query  = mysql_query("SELECT user_id FROM message_partecipant WHERE conversation_id =   '$conversation_id' AND user_id !=$sender");
    while ($row = mysql_fetch_array($query)) {
        $user_id = $row['user_id'];
        print_r($user_id);
    }
}

print_r return the Ids (for instance id100 and id 101)like this:

100101//which is not what i'm trying to do

I have another function that find the username in the database so for each user id I would like to get their usernames in this format:

echo usernameFromId($user_id)// this should echo out all the username like this (user a, user b, user c)

I think I have to do a foreach loop but I can't think how.

5
  • change the SELECT user_id in the fields you want. Commented Oct 1, 2012 at 10:12
  • try mysql_fetch_assoc instead of mysql_fetch_array Commented Oct 1, 2012 at 10:17
  • Your database would love to run JOINs than getting called several times to fetch each user_id's username. Commented Oct 1, 2012 at 11:10
  • Please don't use mysql_* functions in new code. They were removed from PHP 7.0.0 in 2015. Instead, use prepared statements via PDO or MySQLi. See Why shouldn't I use mysql_* functions in PHP? for more information. Commented Oct 1, 2012 at 11:23
  • thanks... I will change the code. Commented Oct 1, 2012 at 12:18

4 Answers 4

1

Try this:

function findOtherUsersInConversation($conversation_id){
    $sender = findMessageSenderId($conversation_id);
    $query =  mysql_query("SELECT user_id FROM message_partecipant WHERE conversation_id =   '$conversation_id' AND user_id !=$sender");
    $users = array();
    while ($row = mysql_fetch_array($query)) {
        $users[] = usernameFromId($row['user_id']); // fetch user name and add it to array
    }
    return implode(', ', $users); // return a string separated by commas
}

findOtherUsersInConversation(10); // conversation id 10
Sign up to request clarification or add additional context in comments.

2 Comments

@hakra his statement does not include i'm trying to build a NEW application, you cannot impose to just move to PDO or MySQLi. To move to a different programming style could take to long.
@MihaiIorga i know that but i think we are supposed to also aware the OP that the mysql_* is not safe anymore ..... you guys got good knowledge so there should be more thing
1

Try like this

function findOtherUsersInConversation($conversation_id) {
$sender = findMessageSenderId($conversation_id);
$query  = mysql_query("SELECT user_id FROM message_partecipant WHERE conversation_id =   '$conversation_id' AND user_id !=$sender");
$cnt=0;
while ($row = mysql_fetch_array($query)) {
    $user_id = $row['user_id'];
   if($cnt==0):
       $comma_separated .=$user_id;
   else: 
      $comma_separated .=",".$user_id;
   endif;   
   $cnt++;
}
 return $comma_separated
}

$getID=findOtherUsersInConversation(10); 

$arrayID= explode( ',', $getID);// split string from comma(,)

print_r($arrayID);// print all ID's as you want

May this will Help you.

Comments

0
function findOtherUsersInConversation($conversation_id){
  $sender = findMessageSenderId($conversation_id);
  $query =  mysql_query("SELECT user_id FROM message_partecipant WHERE conversation_id ='$conversation_id' AND user_id !=$sender");
  $usernameArr = array();
  while ($row = mysql_fetch_array($query)) {
    $user_id= $row['user_id'];
    array_push($usernameArr, usernameFromId($user_id));
  }

  $comma_separated = implode(",", $usernameArr);

  echo $comma_separated;
}

Comments

0

If you want to view the array only for your Information try:

var_dump($array);

otherwise try it in a foreach to output your array:

foreach($array as $var){
    echo $var;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.