dtjzpg5313 2016-10-24 11:30
浏览 8
已采纳

PHP表中的MySQL输出

I am currently designing a product selection page.

I wanna show my users a series of products that I have in my SQL table.

I want to output my SQL into php tables, but I dont want one long table.

I need one table per product.

In short terms,

Product 1, in one table

Break

Product 2, in other table.

<?php
$con=mysqli_connect("localhost","root","","headsets");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM `modeller");
echo "<table border='1'>";
$i = 0;
while($row = $result->fetch_assoc())
{
    if ($i == 0) {
      $i++;
      echo "<tr>";
      foreach ($row as $key => $value) {
        echo "<th>" . $key . "</th>";
      }
      echo "</tr>";
    }
    echo "<tr>";
    foreach ($row as $value) {
      echo "<td>" . $value . "</td>";
    }
    echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>

The sql part works fine, its just the way php output it I want to change

krasipenkovs code

My code

  • 写回答

1条回答 默认 最新

  • drv16759 2016-10-24 11:35
    关注

    Try with this:

    <?php
    $con=mysqli_connect("localhost","root","","headsets");
    // Check connection
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $result = mysqli_query($con,"SELECT * FROM `modeller`");
    
    $i = 0;
    while($row = $result->fetch_assoc())
    {
        echo "<table border='1'>";
        //if ($i == 0) {
          //$i++;
          //echo "<tr>";
          //foreach ($row as $key => $value) {
            //echo "<th>" . $key . "</th>";
          //}
          //echo "</tr>";
        //}
        echo "<tr>";
        foreach ($row as $value) {
          echo "<td>" . $value . "</td>";
        }
        echo "</tr>";
    
        echo "</table><br />";
    }
    mysqli_close($con);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度