Rhea Sanjan
Rhea Sanjan

Reputation: 13

Im trying to retrieve data from mysql database into a html table

Almost everything is working fine except on the first column, the data is preceded by "1".

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

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>

Upvotes: 1

Views: 82

Answers (3)

user8995024
user8995024

Reputation:

It is the simple and easy process, that I will show you as an example and source code.

At first connect database using mysqli in PHP. Using this code : $con=mysqli_connect(‘localhost’,’root’,’password’,’Database Name’);

Now select all data using mysqli from MySQL table in PHP. If you using MySQL query for select record or fetch the record in PHP project, so it’s simple use for you. Fetch result in your table page using while loop.

<?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>

Upvotes: 0

pr1nc3
pr1nc3

Reputation: 8338

 <?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>";


          }
        ?>

Remove the echo from your code.

Upvotes: 0

Nigel Ren
Nigel Ren

Reputation: 57121

The 1 is the result of echoing the return from print... the following lines need to be slightly different..

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

You don't need the echo...

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

Upvotes: 5

Related Questions