dtip91401 2016-08-23 13:11
浏览 10
已采纳

将多个主要Mysql表检索到html表(php)的最佳方法是什么

I'm new to php and I wonder about the best way to retrieve mysql data into html table. Currently I can't provide a well formated code because I'm on phone but let's say the case was like this:

I had mysql table with this structure:

Row -> PK

Column -> PK

Value

I had around 10 Column and a dinamic Rows which need to be displayed in webpage.

My idea is either to loop a small query and echo it, or store it in a big array and scan it rapidly.

First idea:

For ($y = 0; $y < $rowCount; $y++){
Echo "<tr>";
    For ($x = 0; $x < 10; x++){
        $row = Mysqli_query($db,"SELECT Value FROM table WHERE Row='".$y."' AND Column='".$x."'");
        $row = mysqli_fetch_assoc($row);
        Echo "<td>".$row['Value']."<td>";
    }
    Echo "</tr>";
}

Second idea:

$data = Mysqli_query($db,"SELECT * from Table");
For ($y = 0; $y < $rowCount; $y++){
    Echo "<tr>";
    For ($x = 0; $x < 10; $x++){
        While($key = mysqli_fetch_assoc($data)){
            If($key['Row']==$x && $key['Column']==$y)
            Echo "<td>".$row['Value']."<td>";
        }
    }
Echo "</tr>";
}

Sorry if I had wrong syntax because I'm typing it without my syntax cheatsheet and my point is to tell the technique, not asking to debug.

The first Idea are using mysqli query 'WHERE' to look for both primary (Row and Column). Then spam the query in a loop.

The second idea is to store all table result in variable ($data), then keep scanning the variable with While loop.

Note that the the number of Rows is dinamic and could add up to 500 data. That's also the reason why I consider spamming query instead of store everything in variable ($data). But I also wonder if spamming query isn't the best idea because the program would have to keep making connection.

I hope someone could come up with the third idea.

In the real case, my table are more complicated than this and consist around 5 primary key. But the concept are almost equal to the example above.

  • 写回答

1条回答 默认 最新

  • duanhuai7532 2016-08-23 18:33
    关注

    Outside the loop, execute a SELECT with an ORDER BY. Have the loop fetch the rows. Note that it will fetch them in the order already specified, so you don't need that messy if.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑