我的代码说我收到以下错误: fatal error :[] operator not supported for strings in
问题是,我在成员(member)页面上使用了相同的代码,但是当我将它包含在我的相册页面上时,却出现了错误。 PHP:
$sql = "SELECT *, song_genres.genre FROM favoriteband JOIN song_genres ON favoriteband.band_name = song_genres.band_name WHERE favoriteband.username = '$username'";
$query = mysqli_query($conn, $sql); #query
# If user has not favorited bands
$rows_returned = mysqli_num_rows($query);
if ($rows_returned < 1) { ?>
<tr>
<th>For Recommendations,<br> Start Liking Bands</th>
</tr>
<?php } else {
# Loop through each song
while ($row = mysqli_fetch_array($query)){
$username = $row['username']; # Loop Username
$band_name[] = $row['band_name']; # Get the Band Name
$genre[] = $row['genre']; # Get the Genres for each band
}
// The error is fond on $band_name[] = $row['band_name'];
// Also $rows_returned shows 77 rows, so it does produce multiple values.
这是 HTML:
<?php if (isset($_SESSION['username'])){ ?>
<!-- MEMBER RECOMMENDATIONS -->
<table style="width: 90%; margin: 0 auto;">
<tr >
<th style="background: green; color: #fff;" colspan="2">My Recommendations</th>
</tr>
<?php include ("process/memberRecommendation.php"); ?>
</table>
<?php } ?>
成员(member)页面有效,专辑页面无效。这让我感到困惑,因为如果它工作一次,它应该总是工作相同。
最佳答案
$band_name = $row['band_name']; //works
$genre = $row['genre']; //works
然后,如果您将字符串视为数组,它将给出 fatal error :
$bandname[] = 'string'; // fatal error
只需在开头添加这个即可修复该错误
$band_name = array();
$genre = array();
关于php - fatal error : [] Operator not working on one page but working on another,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28643902/