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 ...
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 MATLAB yalmip gurobi求解器
  • ¥15 关于#stm32#的问题:第一张图是发送数据代码,第二第三张图是接收数据并进行DAC转换的函数,若用串口一接收数据并进行DAC转换,主函数代码该怎么写
  • ¥20 hc130怎么读写内部flash存储信息
  • ¥15 Axure rp9注册与登录交互
  • ¥15 我下载图形界面重启完就变成这样了,打字也打不了,动也动不了,该怎么解决(操作系统-centos)
  • ¥15 VBA中在窗体中遍历所有checkbox控件,提取出被选中的checkbox的caption值
  • ¥15 在Ubuntu上有什么命令,或者是系统文件能告诉我链接nvme ssd的pcie槽位是不是支持热插拔功能?
  • ¥15 ansys license许可证问题
  • ¥20 QQ号和密码都能正常登录微信 QQ号和密码登录微信显示密码错误
  • ¥15 单片机RTOS Kernel与应用分离开发,Kernel如何调起应用?