0

This PHP code is for cheking for names who user is following.They are his friends or if they are not his $friends must show <td>&nbsp; &nbsp; &nbsp;<input type="submit" id="downloadbut" value="Follow"></td> if there are his $friends must show echo '<td>&nbsp; &nbsp; &nbsp;<span id="liketextvote">Following</span></td>'; if the user is following them. But my problem is I can't get result for $friends. The problem is

Notice: Array to string conversion in C:\xampp\htdocs\Edu\1111111111111\userinfo.php on line 109 Array

. But inside while loop result is: megiantongeorgekatimery - this is example from MySQL database. I don't have idei how can I get this results. Someone can help?

$userfile = $_GET['username'];
$username = $user_data['username'];

if ($username != $userfile) {
    $addfriends = mysql_query("SELECT `follow` FROM `friends` WHERE `username` = '$username' ORDER BY id DESC");
    $friends = array();

    while ($query_row = mysql_fetch_array($addfriends)) {
        $val = $query_row['follow'];
        echo $val; // hier I can get result example five name megiantongeorgekatimery
        $friends[] = $val;
    }

    echo $friends; // hier I can get result example five name megiantongeorgekatimery

    if (!in_array($userfile, $friends)) {
        echo '<form action="addfriends.php?username='.$userfile.'" method="post">
                  <td>&nbsp; &nbsp; &nbsp;<input type="submit" id="downloadbut" value="Follow"></td>
              </form>';
    } else {
        echo '<td>&nbsp; &nbsp; &nbsp;<span id="liketextvote">Following</span></td>';
    }
}
Peter O.
  • 32,158
  • 14
  • 82
  • 96
user1929805
  • 151
  • 1
  • 1
  • 11
  • So you just want to print out "Following" in a loop? – Explosion Pills Mar 19 '13 at 19:11
  • Is line 109... if (!in_array($userfile, $friends)) {? What is the value of $userfile? – Bafsky Mar 19 '13 at 19:15
  • You have a [huge security](http://en.wikipedia.org/wiki/SQL_injection) hole in there. Read here how to prevent it: http://stackoverflow.com/questions/60174/how-to-prevent-sql-injection-in-php – PeeHaa Mar 19 '13 at 19:17

2 Answers2

4

You are trying to echo an array when you do echo $friends.

If that is just a debug statement, use print_r instead

print_r($friends);
Matt Dodge
  • 10,833
  • 7
  • 38
  • 58
1

You start out by using $friends as an array.
If you want to print the data out from the array, either loop through it using foreach or join in all together before printing.

foreach ($friends as $friend) {print "$friend<br>";}

OR

print join("<br>",$friends);
Trenton Trama
  • 4,890
  • 1
  • 22
  • 27