duansan9435 2018-04-06 11:59
浏览 88
已采纳

Query => Array => Output = PHP警告:非法偏移类型

I try to select mysql data, save them in a two dimensional array and output the data. I get the error message mentioned above for each column and row.

Could someone please point out my mistake. How can I get it to work?

This is the code:

// connect database
    $db_connection = new mysqli (MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB);

    // execute query 
    $query_result = $db_connection->query($sql_career);

    // save carrer data
    while( $row = $query_result->fetch_assoc() )
    {
        $career[]['id'] = $row['career_id'];
        $career[]['season'] = $row['career_season'];
        $career[]['matches'] = $row['career_matches'];
        $career[]['goals'] = $row['career_goals'];
        $career[]['team_name'] = $row['career_team_name'];
    }

    // output
    echo '<table border="1">';
    foreach ($career as $i)
    {
      echo "<tr>";
      echo "<td>". $career[$i]['id'] . "</td>";
      echo "<td>". $career[$i]['season'] . "</td>";
      echo "<td>". $career[$i]['team_name'] . "</td>";
      echo "<td>". $career[$i]['matches'] . "</td>";
      echo "<td>". $career[$i]['goals'] . "</td>";
      echo "</tr>";
    }
    echo "</table>";
  • 写回答

4条回答 默认 最新

  • dongtan7418 2018-04-06 12:16
    关注

    If you have a successful query with rows, then just implode the rows and only loop once.

    if (!$query_result = $db_connection->query($sql_career)) {
        // check error
    } elseif (!$query_result->num_rows) {
        // no rows
    } else {
        echo '<table border="1">';
    
            // I assume you have column headings in mind, add them here
    
            while($row = $query_result->fetch_assoc()) {
                echo "<tr><td>", implode("</td><td>", $row), "</td></tr>";
            }
        echo "</table>";
    }
    

    Or if you have an actual requirement for storing the resultset as a multidimensional array, you can use Miller's top voted comment in the manual.

    for ($set = array (); $row = $query_result->fetch_assoc(); $set[] = $row);
    

    from the above one-liner, you can apply the same "loop and implode" logic like this:

    if (sizeof($set)) {
        echo '<table border="1">';
            foreach ($set as $row) {
                echo "<tr><td>", implode("</td><td>", $row), "</td></tr>";
            }
        echo "</table>";
    }
    

    Or you can write it all out:

    echo '<table border="1">';
        foreach ($set as $row){
            echo "<tr>";
                echo "<td>{$row['career_id']}</td>";
                echo "<td>{$row['career_season']}</td>";
                echo "<td>{$row['career_team_name']}</td>";
                echo "<td>{$row['career_matches']}</td>";
                echo "<td>{$row['career_goals']}</td>";
            echo "</tr>";
        }
    echo "</table>";
    

    And if you want to manipulate the associative keys in advance, you can adjust your SELECT clause like:

    SELECT career_id AS 'id', career_season as 'season', career_team_name AS 'team_name', career_matches AS 'matches', career_goals AS 'goals' FROM ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 MapReduce实现倒排索引失败
  • ¥15 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题