Adding different objects to array, but only one object repeating

Posted by Carpetfizz on Stack Overflow See other posts from Stack Overflow or by Carpetfizz
Published on 2013-11-03T02:40:55Z Indexed on 2013/11/03 3:53 UTC
Read the original article Hit count: 98

Filed under:

I have a small piece of PHP code that goes through valid values and fetches information about them. It then pushes it to an array. For some reason, I'm only getting the last item of $row, repeated several times. When I try to print_r at #1 in the code, the expected values are outputted. However, at the end of the loop, or outside of it, when I try to print_r($ipArray), I'm only getting the last value repeated multiple times. Any help would be much appreciated!

while($row = mysqli_fetch_array($getIpQuery, MYSQLI_NUM)){
    for($x=0;$x<count($row);$x++)
    {

        $getIpInfo = mysqli_query($dbcon, "SELECT * FROM ipInfo WHERE address='$row[$x]'");
        $retrievedInfo  = mysqli_fetch_array($getIpInfo, MYSQLI_NUM);
        $ipInfo->ipAddress = $retrievedInfo[0];
        $ipInfo->portNum = $retrievedInfo[1];
        print_r($ipInfo); //#1: Works perfectly fine. 
        array_push($ipArray,$ipInfo);
    } 
}

print_r($ipArray); //this is where I'm getting an output of only the last element of `$row`. 

Thanks!

~Carpetfizz

© Stack Overflow or respective owner

Related posts about php