Question

Just playing around with different functions, trying to learn as much abut them as I can before I move forward with my self study. Could someone please explain what's going on below? (Please ignore that the query isn't prepared or at all secure)

  $query = "SELECT userid FROM users_groups_memberships WHERE groupid='".$GroupID."'";
  $result = $mysqli->query($relationshipQuery);

       while($row = $relationshipResult->fetch_row()) {
             $existingMembers[]=$row;
       }

  $memberString = implode(', ', $existingMembers);
  echo $memberString;

I'm expecting a comma separated list of numbers like 1, 4, 6, 88, 100 but I'm actually getting Array, Array, Array, Array, Array.

I know this is dead simple because I've had array returned before and I remember it was because I was doing something stupiud, I just don't remember what!

I'm actually trying to see if another variables value is present in $existingMembers using in_array but I get 0 always, probably because the values are array there too.

Thanks in advance for any support :)

Was it helpful?

Solution

$row is an array of the columns returned from the query.

print_r($row) will show what your adding.

Try:

while($row = $relationshipResult->fetch_row()) {
    $existingMembers[]=$row['userid'];//Or $row[0] depending on the format of $row
}

OTHER TIPS

Each row is an array and implode does not join in 2 dimensions.

If you want this to do the output you suggested try changing your statement in the loop to:

$existingMembers[] = $row['userid'];

I know I'm posting this answer a little late, but the use cases for when to use the different fetch_* functions seems to be a little disjointed and unclear. I'll explore the solution based on your sample snippet and some possible alternatives:

Solution

while($row = $relationshipResult->fetch_row()) {
    $existingMembers[]=$row[0];
}

Please review the documentation for fetch_row(). fetch_row() returns a result row as an enumerated array (i.e. $row[0], $row[1], etc).


Alternative Solutions

Dealing with an enumerated array may be confusing from time to time. There are a couple alternatives you can use to avoid the confusion of numbered indexes.

Alternative 1:

while($row = $relationshipResult->fetch_assoc()) {
    $existingMembers[]=$row['userid'];
}

In this example, the result row is returned as an associative array. This makes the coding a bit cleaner and more understandable because it allows you to use the field names requested as the array indexes. In your example, the field name from your SQL query, userid, becomes the key in the associative array and can be accessed using the above notation.

For more information on this alternative, please see the fetch_assoc().

Alternative 2:

while($row = $relationshipResult->object()) {
    $existingMembers[]=$row->userid;
}

In this example, the result row is returned as an object. Again, this makes the coding a bit cleaner and more understandable because it allows you to use the field names requested as the object properties. In your example, the field name from your SQL query, userid, becomes the object property and can be accessed using the above notation.

For more information on this alternative, please see the fetch_object().


Summary

So just to recap, these are the specific use cases of the fetch_* functions associated with mysqli_*. The use cases for when to use the specific functions seemed a little unclear before. I hope this helps clear that up.

Good luck and happy coding! :)

try fetch_field() instead I think it will return the value of the field. I think fetch_row is serving you array('userid'=>1)

change

$existingMembers[]=$row;

to

$existingMembers[]=$row['userid];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top