I am missing something obvious. Here's a trivial piece of PHP, littered with debug echos:
function echo_rows(&$res) {
$rows= array();
while ($row= $res->fetch()) {
echo $row['ccorID'] . "\r\n";
$rows[]= $row;
echo $rows[0]['ccorID'] . "\r\n";
}
echo "---.---\r\n";
echo count($rows) . "\r\n";
foreach($rows as $row) {
echo $row['ccorID'] . "\r\n";
}
echo json_encode($rows);
}
Here's what I see in the response:
0
0
3
3
13
13
182
182
---.---
4
182
182
182
182
It seems perfectly clear to me that :-
- I have 4 rows of data
$rows[]= $row;does NOT perform a value copy, despite what it says here http://uk.php.net/manual/en/language.types.array.php
Anyone got any idea what I need to do to get a copy of $row (which is an associative array)?
Thanks.
EDIT: Since many of you are so insistent to know what $res is, here is the class. I genuinely believe that this is more likely to confuse than enlighten (hence the omission from my OP).
class mysqlie_results {
private $stmt;
private $paramArray= array();
private $assocArray= array();
public function __construct(&$stmt) {
$this->stmt= $stmt;
$meta= $stmt->result_metadata();
while ($colData= $meta->fetch_field()) {
$this->paramArray[]= &$this->assocArray[$colData->name];
}
call_user_func_array(array($stmt,'bind_result'),$this->paramArray);
$meta->close();
}
public function __destruct() {
$this->stmt->free_result();
}
public function fetch() {
return $this->stmt->fetch()? $this->assocArray : false;
}
}
$rowsis being overwritten here.