php - fatal error : [] Operator not working on one page but working on another

标签 php html mysql

我的代码说我收到以下错误: 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/

相关文章:

javascript - PHP - 使用放大弹出窗口提交表单后显示消息

php 使用 ssh 调用 bash 脚本

php - PDO 连接数据库错误

html - 如何在IE中的多个页面上打印网页中的大单个图像

php - 如何保护在 Linux 上运行的 MySQL 服务器?

php - 如何将 Guzzle Mock Handler 传递给 PHP 类以测试具有 json 响应的 API 调用

javascript - IE 中水平滚动的顶部固定菜单

html - 下拉菜单将内容向下推

php - IMAP 和 PHP - 从已发送和收件箱文件夹中获取所有电子邮件

php - 下拉菜单和 MySQl