I have a DB snippit of code here... I'll post the relevant lines without error checking to make it smaller...
if ($stmt->bind_result($row[0], $row[1]) === false) etc...
then below I have...
<pre><code>
//fill the array up with an array of rows, then increment $ddp.
//$data[$ddp] is the row,
//$data[$dpp][0] is the rows first argument,
//$data[$dpp][1] is the rows 2nd argument returned etc...
$ddp = 0; // this increments up every fetch
while ($stmt->fetch()) {
$data[$ddp][0] = $row[0];
$data[$ddp][1] = $row[1];
$ddp++;
}
</code></pre>
The way I have it above WORKS.... but here is how I did it before and something weird was happening...
<pre><code>
$ddp = 0; // this increments up every fetch
while ($stmt->fetch()) {
$data[$ddp++] = $row;
// I ECHOd out $row here... and it did fetch 2 different rows...
}
</code></pre>
What happens is... when I did this...
$data[$ddp++] = $row;
$data[0][0]
was the same as $data[1][0]
.
Why then if $row
had different values on the 2 fetches... how did $data
end up with 2 arrays the same?
I even tried
$data[] = $row;
and the same results. My fix was...
while ($stmt->fetch()) {
$data[$ddp][0] = $row[0];
$data[$ddp][1] = $row[1];
$ddp++;
}
WHY?
Sorry if this isn't the right place to put this but I did find a solution ahead of time and to save time I posted the answer and my question together.
Your two assignments are doing two subtly different things.
while ($stmt->fetch()) {
$data[$ddp][0] = $row[0];
$data[$ddp][1] = $row[1];
$ddp++;
}
This creates two entries in the array - the first will have the index [0][0]
, and be assigned the value in $row[0]
, and so on.
On the other hand, this:
while ($stmt->fetch()) {
$data[$ddp++] = $row;
// I ECHOd out $row here... and it did fetch 2 different rows...
}
is adding a single row to the database, and adding the whole of $row
to $data[0]
on the first run through. So whatever is returned from the database is added as it is - if you're getting an array back from fetch()
, then you're adding an array inside your array.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.