PHP MySQL 查询,用于在一次运行中将字段名称及其数据提取到表中


php mysql query to fetch field names and its data into a table in one run

我试图准备一个数据表,其中传递任何列表查询,其结果都将播种在表中。

我使用了 2 个 if 条件和 foreach 循环。现在我怀疑如果获取批量记录,这会减慢速度。谁能建议我更好的方法来做到这一点..

查询如下

 echo "<table width='100%' align='center' border=1 style='text-align:center; vertical-align:center; border-collapse:collapse; font-size:80%' class='main'>";
    $i=0;
    while($row=mysqli_fetch_array($result)){
      echo "<tr>";
        foreach($row as $rowvalues => $cellvalues){
            if (!is_numeric($rowvalues)){
               if($i==0){ 
                    echo "<th>".$rowvalues."</th>"; // For Table header
               }else{
                    echo "<td>".$cellvalues."</td>"; // For field values
               }
            }
        }
      echo "</tr>"; 
      $i++;
    }
    echo "</table>";

也许这更快一点?(减少回声量)

$output = "<table width='100%' align='center' border=1 style='text-align:center; vertical-align:center; border-collapse:collapse; font-size:80%' class='main'>";
    $i=0;
    while($row=mysqli_fetch_array($result)){
      $output .= "<tr>";
        foreach($row as $rowvalues => $cellvalues){
            if (!is_numeric($rowvalues)){       
                $output .= ($i == 0) ? "<th>".$rowvalues."</th>" : "<td>".$cellvalues."</td>";              
            }
        }
      $output .= "</tr>"; 
      $i++;
    }
    echo $output."</table>";

创建 php 分页以避免在数据库中获取记录的速度变慢。