在PHP

时间:2019-01-15 07:03:50

标签: php html

我有一个代码可以以表格形式打印数据,但是其打印数据在下表中,我想并排打印(请参阅附件图片)

<?php
$sql = "SELECT  player as AM FROM combine where name ='amit mantri';";
$sql .= "SELECT  player as NIk FROM combine where name ='nikhil parab'";
if($connection->multi_query($sql))
    {
        do{
            $result = $connection->store_result();
            $finfo = $result->fetch_fields();
            //echo($finfo['']);
         echo "<table border='1'>";
            foreach($finfo as $f)
            {
                echo "<td>"." ".$f->name."</td>";
            }
            echo "</tr>";
             while($row = $result->fetch_assoc())
            {
                echo "<tr>";
                foreach($row as $v)
                {
                echo "<tr>";

                    echo " "."<td>".$v."</td>";
                }
                echo "</tr>";

            }
            //print_r($finfo);

    } while($connection->more_results() && $connection->next_result());

}
?>

请查看预期结果

它正在像这样显示:

enter image description here

但是应该是这样。...

enter image description here

1 个答案:

答案 0 :(得分:0)

如果您的查询和遍历数据的方式正确,这将为您提供预期的输出。

您的代码缺少一些开始和结束标记,我也将其替换为标题行。

由于缺少标签,您将两列都添加为行。

 <?php
$sql = "SELECT  player as AM FROM combine where name ='amit mantri';";
$sql .= "SELECT  player as NIk FROM combine where name ='nikhil parab'";
if($connection->multi_query($sql))
    {
        do{
            $result = $connection->store_result();
            $finfo = $result->fetch_fields();
            //echo($finfo['']);
         echo "<table border='1'>";
            echo "<tr>";
              foreach($finfo as $f)
              {
                 echo "<th>"." ".$f->name."</th>";

              }
            echo "</tr>";
             while($row = $result->fetch_assoc())
            {
                echo "<tr>";
                foreach($row as $v)
                {


                    echo " "."<td>".$v."</td>";
                    echo " "."<td>".$v."</td>";
                }
                echo "</tr>";

            }
           echo "</table>";
            //print_r($finfo);

    } while($connection->more_results() && $connection->next_result());

}
?>