-2

Im trying something like this:

class kategoria
{
    public $IdKat;
    public $IdKatNad;
    public $NazwaKat;
}

function get_cats()  
{  

    $query = "SELECT IdKat, NazwaKat, OpisKat  FROM `kategorie`"; 

    try
    {
        $stmt = $this->link->prepare($query);

        /* bind parameters for markers */
        //$stmt->bind_param("i", $idKat);

        $stmt->execute();
        $kat = new kategoria();

        $stmt->bind_result(
                $kat->Idkat, 
                $kat->NazwaKat, 
                $kat->OpisKat
                );


        $output = array();

        while ($stmt->fetch()) {
            $output += array(clone $kat);
        }

        $stmt->close();
    }
    catch (Exception $e)
    {
        echo $e;
    }

    echo '<pre>';
    print_r($output);
    echo '</pre>';
}

At the end - i have only last row in array. Why? When i tried with array (and 2 fields), without "kategoria" class - it worked, but i need to put query result in array of objects representing table rows. what im doing wrong? Im new in PHP.

Kamil
  • 13,363
  • 24
  • 88
  • 183

2 Answers2

1
$output += array(clone $kat);

If you're trying to append to the $output array the syntax is.

$output[] = array(clone $kat); // or array_push($output, array(clone $kat));

Adding arrays together in PHP merges them which doesn't seem like what you want.

Mike B
  • 31,886
  • 13
  • 87
  • 111
  • 1
    Some further reading: array_merge() vs array union operator: http://unix0.wordpress.com/2012/01/08/php-array_merge-vs-plus-union-operator/ – biziclop Feb 18 '12 at 07:16
  • I have two doubts here.Why are you using array(clone $kat).Shouldn't it be just ' = clone $kat'.And cloning only does shallow copy.So will this code work in above case? – WordsWorth Feb 18 '12 at 07:17
  • @WordsWorth I just used the same assignment that was in the OP's code. I have no idea what his intentions with it are since he's just `print_r()`'ing it. – Mike B Feb 18 '12 at 07:19
  • array_push is working, array grows, but now i have many objects created from only last row of table. Clone doesnt work? I need to make variables inside my while loop? – Kamil Feb 18 '12 at 07:25
  • @Kamil Because you're looping over the results but cloning the same `$kat` object over and over. You need to assign `$kat` in the loop so it changes before it's appended to the array. – Mike B Feb 18 '12 at 07:27
  • Its the same because im using binding result. – Kamil Feb 18 '12 at 07:34
  • When i used "the same" variable or array in loop - it worked. – Kamil Feb 18 '12 at 07:39
  • I found answer in PHP help. I had to create "deep copy" like this: $clone = unserialize(serialize($object)); Because (i believe) PHP help says about clone something like this: "Any properties that are references to other variables, will remain references." – Kamil Feb 18 '12 at 08:33
0

I had to serialize and deserialize my class binded to result set.

$output += array(deserialize(serialize($kat)));
Kamil
  • 13,363
  • 24
  • 88
  • 183