2017-04-05 27 views
0

我在mysql數據庫中有一個名爲revenue的表,我需要在表格中顯示錶格中的列和數據。到目前爲止,我只是在下面的代碼中回顯數據。幫助表示讚賞。從一個mysql表中顯示數據,使用php

<?php 

    include('adodb/adodb.inc.php'); 
    echo 'To See existing records'; 
    $db=NewADOConnection('mysql');$db->Connect("127.0.0.1", "vpp", "abcd", "vpp"); 


    $sql="select * from revenue"; 
    $result = $db->Execute($sql); 
    if ($result === false) die("failed2"); 
    $records=array(); 
    $count=$result->RecordCount(); 
    echo "Total Records Found :".$count."<br>"; 
    if($count > 0) { 
     for ($x=0;$x<$count;$x++) { 
      $offerId=$result->fields[0]; 
      $affId=$result->fields[1]; 
      $status=$result->fields[2]; 
      $deduction=$result->fields[3]; 


      echo "OfferId:".$offerId." and AffId:".$affId." with deduction %:".$deduction." status=".$status."<br>"; // Need this data to be dispalyed in a table 
      $rec=array("offerId"=>$offerId,"affiliate_id"=>$affId,"status"=>$status, "deduction"=>$deduction); 
      array_push($records,$rec); 
      $result->MoveNext(); 
      } 
    } 
?> 
+0

得到的結果格式化,這樣[SO問題](HTTP顯示在一個HTML表格://計算器。 com/questions/26463923/php-mysql-query-to-html-table-only-inserts-to-first-table-row)?還是我誤解了? – OldPadawan

回答

1

循環您$records陣列和回聲的HTML表格中的行這樣

<table> 
    <thead> 
     <tr> 
      <th> 
       OfferId 
      </th> 
      <th> 
       Affiliate_id 
      </th> 
      <th> 
       status 
      </th> 
      <th> 
       deduction 
      </th> 

     </tr> 
    </thead> 
    <tbody> 
    <?php 
    foreach($records as $row) 
    { 
    ?> 

    <tr> 
     <td> 
     <?php echo $row['offerId']; ?> 
     </td> 
     <td> 
     <?php echo $row['affiliate_id']; ?> 
     </td> 
     <td> 
     <?php echo $row['status']; ?> 
     </td> 
     <td> 
     <?php echo $row['deduction']; ?> 
     </td> 
    </tr> 


    <?php 
    } 
    ?> 

    </tbody> 
    </table>