致命错误:[]操作员不在一个页面上工作但在另一个页面上工作

时间:2015-02-21 08:51:49

标签: php html mysql

我的代码说我收到以下错误:Fatal error: [] operator not supported for strings in

问题是,我在成员页面上使用相同的代码,但是当我将其包含在我的相册页面上时,它会给我错误。 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 } ?>

它适用于会员页面,但不适用于相册页面。它令我感到困惑,因为如果它工作一次,它应该总是一样。

1 个答案:

答案 0 :(得分:0)

$band_name = $row['band_name']; //works
$genre = $row['genre'];   //works

然后,如果将字符串视为数组,则会产生致命错误:

$bandname[] = 'string'; // fatal error

只需在开头添加此内容即可修复该错误

$band_name = array();
$genre = array();