我试图从mysql数据库检索数据到一个HTML表

时间:2017-11-16 08:11:56

标签: php html mysql

几乎所有东西都工作正常,除了第一列,数据前面是“1”。

这是我的输出:
第一列,即strength,前面是1.我的数据库中的数据只是一个数字。

The first column,ie,strength is preceded with 1. The data in my database is just a single digit number.

    <?php
    include 'index.php';
    $sql = "SELECT  t.strength, t.timeslot, t.cust_phno , c.fname, c.lname  FROM tables t,customer c WHERE t.cust_phno = c.cust_phno";
    $result = mysqli_query($con,$sql);
    ?>
    
  <div id="view">
    <table style="width:90%">
      <thead>
      <tr>

        <th> Table Strength </th>
        <th> Time Slot </th>
        <th> Customer Phone Number</th>
        <th> Customer Name </th>
      </tr>
    </thead>
    <tbody>
      <?php
      while( $array = mysqli_fetch_assoc($result)) {
        echo
        print "<tr> <td>";
      echo $array["strength"];
      print "</td> <td>";
      echo $array["timeslot"];

      print "</td> <td>";
      echo $array["cust_phno"];
      print "</td> <td>";
      echo $array["fname"];
      print "&nbsp";
      echo $array["lname"];


      print "</td> </tr>";


          }
        ?>


    </tbody>
  </table>

3 个答案:

答案 0 :(得分:5)

1是回应print的回报的结果...以下几行需要略有不同......

 while( $array = mysqli_fetch_assoc($result)) {
    echo
    print "<tr> <td>";

你不需要回声...

 while( $array = mysqli_fetch_assoc($result)) {
    print "<tr> <td>";

答案 1 :(得分:0)

 <?php
      while( $array = mysqli_fetch_assoc($result)) {
        print "<tr> <td>";
      echo $array["strength"];
      print "</td> <td>";
      echo $array["timeslot"];

      print "</td> <td>";
      echo $array["cust_phno"];
      print "</td> <td>";
      echo $array["fname"];
      print "&nbsp";
      echo $array["lname"];


      print "</td> </tr>";


          }
        ?>

从代码中删除echo

答案 2 :(得分:0)

这是一个简单易行的过程,我将向您展示一个示例和源代码。

首先在PHP中使用mysqli连接数据库。使用此代码:$ con = mysqli_connect('localhost','root','password','Database Name');

现在使用PHP中的MySQL表格中的mysqli选择所有数据。如果您使用MySQL查询选择记录或在PHP项目中获取记录,那么它对您来说很简单。使用while循环在表格页面中获取结果。

&#13;
&#13;
<?php 
include "db_connect.php";
?>
<table class="table">
	<thead class="thead-inverse">
	<tr>
		<th>#</th>
		<th>Name</th>
		<th>Email</th>
		<th>Phone</th>
		<th>Address</th>
	</tr>
	</thead>
	<tbody>
	<?php
	$i=1;
	$mysqli_qry=mysqli_query($con,"SELECT * from `Table Name` ORDER BY `id` DESC");
	while($row=mysqli_fetch_array($mysqli_qry))
	{
	?>
	<tr>
		<th scope="row"><?php echo $i; ?></th>
		<td><?php echo $row['1']; ?></td>
		<td><?php echo $row['3']; ?></td>
		<td><?php echo $row['2']; ?></td>
		<td><?php echo $row['4']; ?></td>
	</tr>
	<?php $i++; } ?>
	</tbody>
</table>
&#13;
&#13;
&#13;