php
  • mysql
  • 2016-11-13 76 views 0 likes 
    0

    我是PHP/MYSQL的新手,我試圖從我的數據庫中使用DATEIFF顯示所有記錄的年齡,如我的代碼的第17行所示,但它只是將無法工作。我需要有人幫助我解決這個問題。PHP/MYSQL使用datediff顯示mysql記錄的年齡

    $result = mysqli_query($con,"SELECT * FROM growers"); 
    
    echo "<table class='table table-striped table-advance table-hover'> 
        <tbody> 
         <tr> 
          <th><i class='icon_profile'></i>&nbsp;Batch</th> 
          <th><i class='icon_ol'></i>&nbsp;Date Received</th> 
          <th><i class='icon_clock_alt'></i>&nbsp;Age when Received</th> 
          <th><i class='icon_clock_alt'></i>&nbsp;Current Age</th> 
          <th><i class='icon_star'></i>&nbsp;NO of Birds</th> 
          <th><i class='icon_info'></i>&nbsp;View More</th> 
         </tr>"; 
    
         while($row = mysqli_fetch_array($result)) 
         { 
          echo"<tr>"; 
          echo"<td>" . $row['BATCH'] . "</td>"; 
          echo"<td>" . $row['BIRTH DAY'] . "</td>"; 
          echo"<td>" . $row['AGE'] . "&nbsp;Week(s)" . "</td>"; 
          echo"<td>" . "SELECT DATEDIFF("NOW()", "$row['BIRTH DAY']") AS CURRENT AGE". "</td>"; 
          echo"<td>" . $row['NO OF BIRDS'] . "</td>"; 
          echo"<td>" . $row['AGE'] . "</td>"; 
          echo"</tr>"; 
         } 
    echo "</table>"; 
    
    mysqli_close($con); 
    ?> 
    
    +1

    行'回聲 「​​」。 「SELECT DATEDIFF(」NOW()「,」$ row ['BIRTH DAY']「)as CURRENT AGE」。 「」'真的很混亂。您正試圖執行查詢並輸出結果,但實際上只顯示查詢。根據@ scaisEdge的回答建議,在第一行中計算查詢中的DATEDIFF()。 – karliwson

    回答

    1

    你可以在主選擇選擇

    $result = mysqli_query($con, 
         "SELECT 
         growers.BATCH, 
         growers.`BIRTH DAY`, 
         growers.AGE,    
         DATEDIFF(NOW(),growers.`BIRTH DAY`) AS CURRENT_AGE, 
         growers.`NO OF BIRDS`    
         FROM growers; " 
        ); 
    
    
             echo "<table class='table table-striped table-advance table-hover'> 
             <tbody> 
             <tr> 
             <th><i class='icon_profile'></i>&nbsp;Batch</th> 
             <th><i class='icon_ol'></i>&nbsp;Date Received</th> 
             <th><i class='icon_clock_alt'></i>&nbsp;Age when Received</th> 
             <th><i class='icon_clock_alt'></i>&nbsp;Current Age</th> 
             <th><i class='icon_star'></i>&nbsp;NO of Birds</th> 
             <th><i class='icon_info'></i>&nbsp;View More</th> 
             </tr>"; 
    
             while($row = mysqli_fetch_array($result)) 
             { 
             echo"<tr>"; 
             echo"<td>" . $row['BATCH'] . "</td>"; 
             echo"<td>" . $row['BIRTH DAY'] . "</td>"; 
             echo"<td>" . $row['AGE'] . "&nbsp;Week(s)" . "</td>"; 
             echo"<td>" . $row['CURRENT_AGE'] . "</td>"; 
             echo"<td>" . $row['NO OF BIRDS'] . "</td>"; 
             echo"<td>" . $row['AGE'] . "</td>"; 
             echo"</tr>"; 
             } 
             echo "</table>"; 
    
             mysqli_close($con); 
             ?> 
    
    +0

    謝謝scaisEdge。但是我申請了這個建議後,我的桌子什麼都沒顯示。 –

    +0

    沒有爲CURRENT_AGE或沒有爲所有的領域? – scaisEdge

    +0

    整個表消失了,除了標題 –

    相關問題