4

I have 2 arrays:

$arr = [];

$tempArray = [
    'val1' => "xxx",
    'val2' => 0,
    'val3' => 0
];

Then in my mysql query i fill the temp array with values from the current row and finally push him into the $arr:

$stmt->bind_result($tempArray["val1"], $tempArray["val2"], $tempArray["val3"]);

while ( $stmt->fetch () ) {
    array_push($arr, $tempArray);
}

The Problem is, on every loop the "array_push" overrides the data in the $arr.

For example I loop 3 times in the $stmt->fetch().

1. Loop

$tempArray = [
    'val1' => "Hello",
    'val2' => 1,
    'val3' => 2
]

$arr = [
    0 = [
        'val1' => "Hello",
        'val2' => 1,
        'val3' => 2
    ];
]

2. Loop

$tempArray = [
    'val1' => "Stack",
    'val2' => 3,
    'val3' => 4
]

$arr = [
    0 = [
        'val1' => "Stack",
        'val2' => 3,
        'val3' => 4
    ],
    1 = [
        'val1' => "Stack",
        'val2' => 3,
        'val3' => 4
    ];
]

3. Loop

$tempArray = [
    'val1' => "Overflow",
    'val2' => 5,
    'val3' => 6
]

$arr = [
    0 = [
        'val1' => "Overflow",
        'val2' => 5,
        'val3' => 6
    ],
    1 = [
        'val1' => "Overflow",
        'val2' => 5,
        'val3' => 6
    ],
    2 = [
        'val1' => "Overflow",
        'val2' => 5,
        'val3' => 6
    ]
]

I never saw this behavior before and i don't know why it does this.

What i want at the end is this:

$arr = [
    0 = [
        'val1' => "Hello",
        'val2' => 1,
        'val3' => 2
    ],
    1 = [
        'val1' => "Stack",
        'val2' => 3,
        'val3' => 4
    ],
    2 = [
        'val1' => "Overflow",
        'val2' => 5,
        'val3' => 6
    ]
]

$stmt class (requested from @Stevish)

$query = "...";
if ( $stmt = $this->db->prepare($query)) {
        $stmt->bind_param('i',  $xxx);
        $stmt->execute();
        $stmt->store_result();
        $$stmt->bind_result($tempArray["val1"], $tempArray["val2"], $tempArray["val3"]);
        while ( $stmt->fetch () ) {
            $arr[] = $tempArr;
        }
    }
1

2 Answers 2

0

The issue is that you are inserting a reference to $tempArray into $arr. Then you change the reference. By the third loop you have 3 references to the same array. That is why the values are showing that way... you can solve this in a rather non intuitive way.

try:

$stmt->bind_result($tempArray["val1"], $tempArray["val2"],$tempArray["val3"]);
while ( $stmt->fetch () ) {
    $x = $tempArray; //This copies the values of $tempArray to $x and each loop will create a new x.
    array_push($arr, $x);
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is never going to work. It doesn't solve the problem with references.
0

Try:

while ( $stmt->fetch () ) {
    $arr[] = $tempArray;
}

The [] after the variable simply lets PHP know that you're adding a new entry to the array. It will be given a numerical value (0, 1, 2) as you require.

6 Comments

Same behavior as before, data gets overwritten.
Ah, I see. I think it's in your $stmt->fetch() function that this is happening. Could you post the code for the $stmt class?
Just looked at the man page and saw that your code and mine did the exact same thing, mine just had lower overhead
i've added my class in the question above :)
Sorry, mate. This is too far outside my area of knowledge. I'm not sure how to work with all that binding, executing and storing. I've not used those commands before. I use an entirely different approach
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.