0

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>
3
  • undefined variable: sql in C:\wamp\www\Doctors\approval.php on line Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in C:\wamp\www\Doctor\approval.php Commented Mar 26, 2017 at 7:04
  • @s.ravi did you try the solutions answered below? Commented Mar 26, 2017 at 7:07
  • @s.ravi check the answer.. and go through mysqli Commented Mar 26, 2017 at 7:10

3 Answers 3

1

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))
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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();
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.