PDO Fetching Data To Object Issue

Itamar

I'm using the following code in order to get data from table and fetch it to object named Question :

$stmt = $conn->query("SELECT * FROM QA");
$stmt->setFetchMode(PDO::FETCH_INTO,new QA);

foreach($stmt as $qa){
    var_dump($qa);
    array_push($this->allQA,$qa);
}

The code above outputs :

object(QA)[5]
  public 'question' => string 'first question' (length=14)
  public 'answer' => string 'first answer' (length=12)
object(QA)[5]
  public 'question' => string 'second question' (length=15)
  public 'answer' => string 'second answer' (length=13)

However, var_dump($this->allQA); outputs :

array (size=2)
  0 => 
    object(QA)[5]
      public 'question' => string 'second question' (length=15)
      public 'answer' => string 'second answer' (length=13)
  1 => 
    object(QA)[5]
      public 'question' => string 'second question' (length=15)
      public 'answer' => string 'second answer' (length=13)

Why does it happen ? why "first question first answer" doesn't appear at all while "second question second answer" appears twice?

Thanks in advance.

Max

Objects are by default passed by reference when pushing them along to the array, so when you modify the original object, all instances that you've put into the array will become modified too.

Here's where it goes wrong:

array_push($this->allQA, $qa);

For every loop, the variable $qa receives the value of the next element of $stmt. Since the values previously pushed onto the array have been pushed as references, it means that any modifications to $qa will affect the previous values too. This results in all the values being equal to the last modification you did to $qa, which is the second answer.

To fix it, you want to push a copy of the object to the array, and not a reference. This can be accomplished using the clone-keyword:

array_push($this->allQA, clone $qa);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PDO Fetching Data To Object Issue

From Dev

Php and PDO - fetching data

From Dev

Fetching data issue

From Dev

Issue fetching data in SQLite

From Dev

issue in fetching data in MySQL

From Dev

Service workers issue in fetching the data

From Dev

Issue with inserting data PDO

From Dev

Fetching Data from DB using PDO with Class

From Dev

Issue With RETS Data Fetching Feed in PHP

From Dev

Issue with fetching data from excel (Using Java)

From Dev

Fetching data issue using simple like query

From Dev

Fetching data issue from mysql database in this code

From Dev

Issue With RETS Data Fetching Feed in PHP

From Dev

While and foreach different behaviour with fetching PDO::FETCH_OBJECT

From Dev

get row count in pdo class - after fetching data

From Dev

PHP/PDO - Fetching data from two tables in loop

From Dev

Is it possible to pass field name as $variable while fetching data PDO PHP?

From Dev

Fetching corrupted data from MySQL DB using PDO in php

From Dev

Fetching corrupted data from MySQL DB using PDO in php

From Dev

PHP/PDO - Fetching data from two tables in loop

From Dev

fetching a custom object from core data into an array

From Dev

Fetching data from Key Value JSON object

From Dev

PHP PDO Fetching Results

From Dev

PHP remote MySQL data fetching size limit issue

From Dev

Issue in fetching data from db to the views in laravel 5.2

From Dev

PHP remote MySQL data fetching size limit issue

From Dev

Fetching inside if statement using PDO

From Dev

PDO fetching results from id

From Dev

Fetching results from database in PDO