Reputation: 23
If I have an array $MovieDetails = array();
and it is populated by the query below with a foreach loop (5 elements; id, movie_year, genre, image, movie_name
), how do I add another element (movie_rating
) to the end of the array
$AllMovies = $con ->query
("
SELECT id, movie_year, genre, image, movie_name FROM movies;
");
while($row = $AllMovies->fetch_object()) {
$MovieDetails[] = $row;
}
Upvotes: 0
Views: 71
Reputation: 27092
Add movie rating into $row
.
If you work with that as object, it's $row->movie_rating = 1.5
while($row = $AllMovies->fetch_object()) {
$row->movie_rating = 1.5;
$MovieDetails[] = $row;
}
If you work with that as array, use fetch_assoc()
and $row['movie_rating'] = 1.5
while($row = $AllMovies->fetch_assoc()) {
$row['movie_rating'] = 1.5;
$MovieDetails[] = $row;
}
Upvotes: 3
Reputation: 1356
This way your row is an object
$AllMovies = $con->query("SELECT id, movie_year, genre, image, movie_name FROM movies;");
while($row = $AllMovies->fetch_object()) {
$row->movie_rating = 'movieRating';
$MovieDetails[] = $row;
}
If you want each row to be array, you should do:
while($row = $AllMovies->fetch_array()) {
$row['movie_rating'] = 'movieRating';
$MovieDetails[] = $row;
}
Upvotes: 1