s.ravi
s.ravi

Reputation: 92

php code to retrieve data from mysql database and display in html table

I want to retreive information in MySQL database and display it html table.when i try with this code i get error message.i cant resolve that.i am new to php.help me. here is my code

     <?php
     session_start();
     ?>

     <?php
     $conn = mysqli_connect("localhost","root","","doctor");

     if (mysqli_connect_errno())
     {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
     }
     if (isset($_POST['button'])) {

     $sql="select Mid,Mname,Mnic,amount,month,bank from payments ";
     }
     $conn->close();
     ?>

     <!DOCTYPE html>
     <html lang="en">
     <head>
     <title>Approvals</title>
     </head>
     <body>

     <div class="container">
     <div class="row">


     <div class="col-md-12">

        <br>
        <br>
        <div class="table-responsive">


            <table id="mytable" class="table table-bordred table-striped" border="1">

                <thead style="background-color: sandybrown">

                <th><input type="checkbox" id="checkall" /></th>
                <th>Doctor ID</th>
                <th>Doctor Name</th>
                <th>Payment Type</th>
                <th>Mobile Number</th>
                <th>Email</th>
                <th>Pay Date</th>
                </thead>
                <tbody>

                <tr>
                    <td><input type="checkbox" class="checkthis" /></td>
                <?php
                while( $row = mysqli_fetch_assoc($sql)): ?>

                    <td > <?php echo $row['Mid']; ?> </td >
                    <td > <?php echo $row['Mname']; ?></td >
                    <td > <?php echo $row['Mnic']; ?> </td >
                    <td > <?php echo $row['amount']; ?></td >
                    <td > <?php echo $row['month']; ?> </td >
                    <td ><?php echo $row['bank']; ?></td >
                </tr >

                <?php endwhile ?>

                </tbody>

            </table>

            <div class="form-group " align="center">
                <a style="box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px          20px 0 rgba(0,0,0,0.19);border: 2px solid #4CAF50;background-color: white;color: black;width: 15%; float: right" href="#" target="_blank" type="button" id="button" name="button" class="btn btn-primary btn-lg btn-block login-button">Submit</a>
             </div>


           </div>
           </div>
           </div>
           </body>
           </html>

Upvotes: 0

Views: 2384

Answers (3)

Suraj Rao
Suraj Rao

Reputation: 29635

I agree with the other answers but before that you need to actually query.

your $sql="select Mid,Mname,Mnic,amount,month,bank from payments "; is just a string within the if block.

set

$sql="";

outside.

if (isset($_POST['button'])) {

 $sql="select Mid,Mname,Mnic,amount,month,bank from payments ";
 }

And call

 $res = mysqli_query($conn,$sql);

and

 while( $row = mysqli_fetch_assoc($res))

Upvotes: 1

Gerrit
Gerrit

Reputation: 651

You closed the connection too early. The connection must be open while iterating over the items.

Upvotes: 1

kubre
kubre

Reputation: 166

Remove this line :

conn->close();

if this do not works specify that error clearly.

and add this line after endwhile after

<?php endwhile;
mysqli_close();
?>

Upvotes: 0

Related Questions