追加到数组会替换数组中的所有元素

I'm building an array of rows from a DB using the code below.

$site = new stdClass();    
$dbh = _vic_commander_db();
    $sth = $dbh->prepare('SELECT name, title, url FROM site ORDER BY siteid');
    $sth->bindColumn(1, $site->name);
    $sth->bindColumn(2, $site->title);
    $sth->bindColumn(4, $site->url);

    $sth->execute();
    $ret = array();
    while ($sth->fetch(PDO::FETCH_BOUND))
    {
        $ret[] = $site;
    }

The array ($ret[]) does get added to each iteration of the loop; however in addition to APPENDING each of the rows of the table, all elements get REPLACED by the last result appended. So I have an array with the same number of elements as rows in the table but the elements are all the same.

Any ideas would be much appreciated.

Example out:

    array(3) (
      [0] => stdClass object {
        name => (string) Same Site Name
        title => (string) Same Site Title
        url => (string) samepurl.com
      }
      [1] => stdClass object {
        name => (string) Same Site Name
        title => (string) Same Site Title
        url => (string) samepurl.com
      }
      [2] => stdClass object {
        name => (string) Same Site Name
        title => (string) Same Site Title
        url => (string) samepurl.com
        }
  )

The other answer has a decent explanation, but why not just fetch into an array of objects?

$sth = $dbh->prepare('SELECT name, title, url FROM site ORDER BY siteid');
$ret = $sth->fetchAll(PDO::FETCH_OBJ);

All the array indexes point to the same object $site.

You have to copy by value instead of by reference:

$ret[] = clone $site;

see http://php.net/manual/en/language.oop5.cloning.php

Also I wonder why you really need to store an object here.

try this way, according to documentation ( execute() before bindColumn() ):

$site = new stdClass();    
$dbh = _vic_commander_db();
    $sth = $dbh->prepare('SELECT name, title, url FROM site ORDER BY siteid');
    $sth->execute();

    $sth->bindColumn(1, $site->name);
    $sth->bindColumn(2, $site->title);
    $sth->bindColumn(4, $site->url); //could you explain why 4?? not 3 ???

    $ret = array();
    while ($sth->fetch(PDO::FETCH_BOUND))
    {
        $ret[] = $site;
    }