0

So I have this query that is supposed to generate a sentance saying "conversation between x and x' but right now, I am only getting it to print the first two digits of those users userids. Ultimately I will change the query to grab their usernames, but I am just starting with userid's for now. As the array is in strings, I think the same issue would be happening if I was grabbing the usernames.

Also, I know it's deprecated... I plan on switching to sqli shortly. Should have no effect on this. The query works fine, i think it's just grabbing from the array wrong.

 $sql1 = "SELECT distinct(userid) as userid FROM message_recips WHERE messageid=$_GET[id]";
       echo $sql1;
       $result1=mysql_query($sql1);
       var_dump($result1);
       $results2=mysql_fetch_array($result1);
       var_dump($results2);
       $uids= array();
       foreach ($results2 as $result) {
                $uids[] = (int)$result['userid'];
       }
       $last = array_pop($uids);

       print '<p>Conversation between ';
       print implode(', ', $uids) . ' and ' . $last;
       echo '.</p>';

Here is what it is printing from the array:

 Conversation between 2 and 2.

when the array looks like :

array (size=2)
0 => string '274' (length=3)
userid' => string '274' (length=3)

Sincere thanks for any help! I have looked around tried a bunch of things that from similar issues but cannot find one for mine.

ambe5950
  • 75
  • 2
  • 10
  • do a print_r($result, true); after each foreach loop. – Marshall Tigerus Nov 13 '14 at 19:30
  • 1
    Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). You will also want to [Prevent SQL Injection!](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Nov 13 '14 at 19:30
  • also the code $uids[] = (int)$result['userid'] replaces itself every loop instead of adding to the array like I think you intend. – Marshall Tigerus Nov 13 '14 at 19:31

2 Answers2

0

Change

$uids[] = (int)$result['userid'];

to

$uids[] = $result['userid'];
Joe Murray
  • 545
  • 4
  • 19
  • made this change, thanks. I had that in there as I was just tinkering with stuff to try to make it work. I got the answer using a while loop. – ambe5950 Nov 13 '14 at 21:19
0
//$results2=mysql_fetch_array($result1);
$uids = array();
while ($row = mysql_fetch_assoc($result1)) {
    $uids[] = $row['userid'];
}
$last = array_pop($uids);
Michael Freund
  • 234
  • 1
  • 10
  • Hey, I didn't see your post before I fixed it, but that is exactly what I did to make it work. Thanks! – ambe5950 Nov 13 '14 at 21:20