JOIN query only returning 1st row
I'm trying to retrieve multiple rows from joining two tables where store.itemid = item_list.id.
$query = "SELECT s.price, il.*
FROM store s LEFT JOIN item_list il ON s.itemid = il.id";
I then have:
if($result = $conn->($query)) {
$array = $result->fetch_array(MYSQLI_ASSOC);
}
With my current code, the query is only retrieving the first row from the 'store' table. I have mad开发者_开发技巧e certain that there should definitely be more than one row to return.
print_r($array) shows:
Array ( [price] => 400 [id] => 5 [name] => Computer )
That's because you are only running fetch_array() once. You probably need to run it in a loop, e.g.:
if ($result = $conn->query($query)) {
while ($array = $result->fetch_array(MYSQLI_ASSOC)) {
// do something with $array
}
}
instead of this:
$array = $result->fetch_array(MYSQLI_ASSOC);
use this:
while($row = $result->fetch_array(MYSQLI_ASSOC)){
//put your code here!
}
精彩评论