2017-03-26 187 views
0

我想在MySQL數據庫中檢索信息並顯示它html table.when我試着用這個代碼我得到錯誤message.i不能解決that.i是新的php.help我。這裏是我的代碼php代碼從mysql數據庫檢索數據並顯示在html表中

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

未定義的變量:SQL在C:\瓦帕\ WWW \醫生\ approval.php線警告:mysqli_fetch_assoc()預計參數1是mysqli_result,null在C:\ wamp \ www \ Doctor \ approval.php中給出 –

+0

@ s.ravi您是否嘗試過下面解答的解決方案? – user3284463

+0

@ s.ravi檢查答案..並通過[mysqli](http://php.net/manual/en/book.mysqli.php) –

回答

1

我與其他答案同意添加ENDWHILE後,這條線,但在這之前,你需要實際查詢

您的$sql="select Mid,Mname,Mnic,amount,month,bank from payments ";只是if block中的一個字符串。

設置

$sql=""; 

之外。

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

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

並調用

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

while($row = mysqli_fetch_assoc($res)) 
+0

非常感謝你完美的作品.... –

+0

很高興它解決..標記爲解決,如果它幫助:) –

1

你太早關閉了連接。迭代項目時,連接必須打開。

0

刪除此行:

conn->close(); 

如果這不作品中明確規定該錯誤。

<?php endwhile; 
mysqli_close(); 
?> 
相關問題