I have a foreach MySQL query that I expect to return all results with an identical order ID, yet it yields the same product ID. It knows the correct amount of entries with that particular order ID, as the counter stops after running through all entries with the same order ID.
My foreach statement:
$i = 0;
foreach ($_SESSION["cart_array"] as $per_item)
{
$item_id = $per_item['latestid'];
$fetchorder = mysql_query("SELECT ISBN FROM transactions WHERE Order_ID='$latestid'");
while ($row = mysql_fetch_array($fetchorder))
{
$ISBN = $row["ISBN"];
};
echo "ISBNs in order $latestid are: $ISBN";
echo "<br>";
$i++;
};
$latestid is obtained from a previous query, that correctly results in collecting the most recent order ID.
My database table looks like this:
Table: transactions
Order_ID | ISBN
25 | 11111111
25 | 22222222
25 | 33333333
Yet, my yielded results are:
latestid = 25
ISBNs in order 25 are: 33333333
ISBNs in order 25 are: 33333333
ISBNs in order 25 are: 33333333
(The latestid = 25 is echoed from the previous sql query, just for testing purposes to ensure it works correctly.)
What's causing this to display the same item ID (ISBN) when it should list all from that particular order ID?
Thanks in advance,
Jamie.
var_dump($_SESSION);?mysql_*-functions. They have been deprecated since PHP 5.5 (in 2013) and were completely removed in PHP 7 (in 2015). Use MySQLi or PDO instead.ISBN = $row["ISBN"];<- You are overwriting that variable in each iteration so it will only contain the ISBN from the last iteration when you echo it (which you do after the loop).