phparraysresultset

How to access a single column value while fetching result set rows


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.


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
    }