【问题标题】:Generating an HTML table with PHP [closed]使用 PHP 生成 HTML 表格 [关闭]
【发布时间】:2016-03-31 05:13:54
【问题描述】:

我遇到了 php 的问题,我有这张表:

    <?php 
     $s=(" SELECT * FROM my_tbl");
     $W=mysql_query($s); 
     ?> 
     <table>
     <tr>
     <td> <center><strong>Member </strong></center></td> 
</tr><tr>
     <td> <center><strong> Total Meal </strong></center></td> 
     </tr> 
     <?php while ( $r=mysql_fetch_array($W)) {$i++; ?>
     <tr > <td> <?= $r["name"]; ?> </td></tr>
<tr>
     <td> <?= $r["tmeal"]; ?> </td>
     </tr><?php } ?>
     </table>

但我正在寻找这个输出:

我怎样才能改进我的代码才能得到这个结果?

【问题讨论】:

  • 这些数据是否来自数据库?请展示您的尝试。
  • 是的,这些数据来自数据库.. 对象操纵器
  • 你现在的密码是什么
  • 是的,同意@RemyaR 到目前为止你尝试了什么?
  • 你的 html & php 代码在哪里?

标签: php html css


【解决方案1】:
 $conn = mysqli_connect('server','username','password',"DB NAme") or die("error");

 $queryString = "select * from TableNAme";
 $queryres    = mysqli_query($conn, $queryString) or die("not fire");

 echo "<table>";
 while($row = mysqli_fetch_array($queryres))
 {
   echo "<tr><td>$row[0]</td><td>$row[0]</td><td>$row[0]</td></tr>";
 }
 echo "</table>";

【讨论】:

  • mysql 扩展已弃用,未来将被移除,请使用 mysqli 或 PDO 代替 mysql
  • 是的,先生,我同意 Devs 先生的观点
【解决方案2】:

这可能对你有帮助

 <?php
  $con = mysql_connect("localhost", "root", "mypass") or  
      die("Could not connect: " . mysql_error());  
  mysql_select_db("your_db"); 
  $s=(" SELECT * FROM my_tbl");
  $W=mysql_query($s); ?>
  <table>
  <tr>
  <td> <center><strong>Member </strong></center></td> 
  <td> <center><strong> Total Meal </strong></center></td> 
  </tr> 
  <?php while ( $r = mysql_fetch_assoc($W)) { ?>
  <tr > <td> <? php echo $r['name']; ?></td> 
  <td> <? php echo $r['tmeal']; ?>  </td> 
   </tr><?php   } ?>
  </table>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-08
    • 2010-09-20
    相关资源
    最近更新 更多