2012-10-10 80 views
-1

好吧,我的問題是:爲什麼我的MySQL數據沒有顯示在我的表中?一切似乎工作正常,但我的數據(這是一個鳥名單等)沒有顯示出來。我需要一些能看清我的錯誤的新眼睛。是的,我知道有可能更簡單的方法來做到這一點,但這是我的任務所需要的,所以請不要提供其他方式來做到這一點。我需要的只是幫助我的數據填充到HTML表格中。謝謝你,傑森我的PHP代碼如下:爲什麼我的MySQL數據沒有顯示在我的表中?

PHP代碼

<?php 
    $pageTitle = 'Mod06 Pagination| Jason McCoy '; 
    include('includes/header.inc.php'); 
    include ('includes/db_connect.inc.php'); 

    $display = 8; 

    // Determine how many pages there are... 

    if (isset($_GET['p']) && is_numeric($_GET['p'])) { // Already been determined. 
     $pages = $_GET['pages']; 
    } else { 
     $query = "SELECT COUNT(birdID) FROM birds"; 
     $result = @mysqli_query($dbc, $query); 
     $row = @mysqli_fetch_array($result, MYSQLI_NUM); 
     $records = $row[0]; 
    } 

    // Calculate the number of pages... 

    if ($records > $display) { 
     $pages = ceil($records/$display); 
    } else { 
     $pages = 1; 
    } 

    // Determine where in the database to start returning results... 

    if (isset($_GET['s']) && is_numeric($_GET['s'])) { 
     $start = $_GET['s']; 
    } else { 
     $start = 0; 
    } 

    // Sort the columns 
    // Default is birdID 

     $sortDefault = 'birdID'; 

     // Create an array for the columns 

     $sortColumns = array('birdID', 'nameGeneral', 'nameSpecific', 'populationTrend'); 

     // Define sortable query ASC DESC 

     $sort = (isset($_GET['sort'])) && in_array($_GET['sort'], $sortColumns) ? $_GET['sort']: $sortDefault; 
     $order = (isset($_GET['order']) && strcasecmp($_GET['order'], 'DESC') == 0) ? 'DESC' : 'ASC'; 

     // Run the query 

    $query = "SELECT birdID, nameGeneral, nameSpecific, populationTrend FROM birds ORDER BY $order LIMIT $start, $display"; 
    $result = @mysqli_query($dbc, $query);  
?> 

    <!-- Table header: --> 
    <table align="center" cellspacing="0" cellpadding="5" width="80%"> 
     <tr> 
      <th><a href='index.php?sort=birdID&order=<?php echo $order == 'DESC' ? 'ASC' : 'DESC' ?>'>Bird<? 
          if($_GET["order"]=="ASC" && $_GET["sort"]=="birdID"){ 
           echo '<img src="images/downArrow.jpg" id="birdASC" name="birdASC" style="margin:-15px 0 0 13px;" width="18px" height="18px">'; 
          } else { 
        echo '<img src="images/upArrow.jpg" id="birdDESC" name="birdDESC" style="margin:-15px 0 0 13px;" width="18px" height="18px">'; 
       }?></a></th> 
      <th><a href='index.php?sort=nameGeneral&order=<?php echo $order == 'DESC' ? 'ASC' : 'DESC' ?>'>General Name<? 
          if($_GET["order"]=="ASC" && $_GET["sort"]=="nameGeneral"){ 
           echo '<img src="images/downArrow.jpg" id="nameGeneralASC" name="nameGeneralASC" style="margin:-15px 0 0 13px;" width="18px" height="18px">'; 
          } else { 
        echo '<img src="images/upArrow.jpg" id="nameGeneralDESC" name="birdDESC" style="margin:-15px 0 0 13px;" width="18px" height="18px">'; 
       }?></a></th> 
      <th><a href='index.php?sort=nameSpecific&order=<?php echo $order == 'DESC' ? 'ASC' : 'DESC' ?>'>Name Specific<? 
          if($_GET["order"]=="ASC" && $_GET["sort"]=="nameSpecific"){ 
           echo '<img src="images/downArrow.jpg" id="nameSpecificASC" name="nameSpecificASC" style="margin:-15px 0 0 13px;" width="18px" height="18px">'; 
          } else { 
        echo '<img src="images/upArrow.jpg" id="nameSpecificDESC" name="birdDESC" style="margin:-15px 0 0 13px;" width="18px" height="18px">'; 
       }?></a></th> 
      <th><a href='index.php?sort=populationTrend&order=<?php echo $order == 'DESC' ? 'ASC' : 'DESC' ?>'>Population Trend<? 
          if($_GET["order"]=="ASC" && $_GET["sort"]=="populationTrend"){ 
           echo '<img src="images/downArrow.jpg" id="populationTrendASC" name="populationTrendASC" style="margin:-15px 0 0 13px;" width="18px" height="18px">'; 
          } else { 
        echo '<img src="images/upArrow.jpg" id="populationTrendDESC" name="birdDESC" style="margin:-15px 0 0 13px;" width="18px" height="18px">'; 
       }?></a></th> 
     </tr> 
<?php 

    // Display the database results in the table... 

    while ($row = @mysqli_fetch_array($result, MYSQL_ASSOC)) { 
     echo '<tr> 
      <td align="left">$row[birdID]</td> 
      <td align="left">$row[nameGeneral]</td> 
      <td align="left">$row[nameSpecific]</td> 
      <td align="left">$row[populationTrend]</td> 
     <tr>'; 
    } 

    echo '</table>';  
    mysqli_close($dbc); 

    // Make the links to other pages, if necessary. 
    if ($pages > 1) { 
     echo '<br /><p>'; 
     $currentPage = ($start/$display) + 1; 
    // If it's not the first page, make a Previous button: 
    if ($currentPage != 1) { 
     echo '<a href="index.php?s=' . ($start - $display) . '&pages=' . $pages . '&sort=' . $sort . '">Previous</a> '; 
    } 
    // Make all the numbered pages: 
    for ($i = 1; $i <= $pages; $i++) { 
     if ($i != $currentPage) { 
      echo '<a href="index.php?s=' . (($display * ($i - 1))) . '&pages=' . $pages . '&sort=' . $sort . '">' . $i . '</a> '; 
     } else { 
      echo $i . ' '; 
     } 
    } // End of FOR loop. 

    // If it's not the last page, make a Next button: 
    if ($currentPage != $pages) { 
     echo '<a href="index.php?s=' . ($start + $display) . '&pages=' . $pages . '&sort=' . $sort . '">Next</a>'; 
    } 

    echo '</p>'; 

} 

    include('includes/footer.inc.php'); 
?> 


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

回答

0

變化這一個$row[birdID]$row['birdID']所有,你錯過了''

+0

感謝您的幫助,但這不起作用,它會返回錯誤 – HoppedUpDesigns

+0

您收到了什麼錯誤? –

+0

如果您想查看目前顯示的內容,請點擊此處鏈接:http://www.jasonkmccoy.com/AB-Tech/web250/Mod06/McCoy_mod06/index.php?sort=nameSpecific&order=ASC I got this錯誤解析錯誤:語法錯誤,意外的T_ENCAPSED_AND_WHITESPACE,期待T_STRING或T_VARIABLE或T_NUM_STRING在92行/home/jkm1972/public_html/AB-Tech/web250/Mod06/McCoy_mod06/index.php – HoppedUpDesigns

0

使用"內部回聲這樣,你已經使用'因爲不顯示從數據庫中的值。

while ($row = @mysqli_fetch_array($result, MYSQL_ASSOC)) { 
    echo "<tr> 
     <td align=\"left\">$row[birdID]</td> 
     <td align=\"left\">$row[nameGeneral]</td> 
     <td align=\"left\">$row[nameSpecific]</td> 
     <td align=\"left\">$row[populationTrend]</td> 
    <tr>"; 
} 
0

對不起,如果有點簡潔,從輸入電話號碼...

'$行[birdID]'

和其他部分應適當改寫爲:

「$行['birdID']'

as birdID是一個字符串,而不是一個變量名。

雖然PHP使用它作爲字符串,但如果在當前作用域中沒有該名稱的變量。

Always use quotes around a string literal array index. For example, $foo['bar'] is correct, while $foo[bar] is not. But why? It is common to encounter this kind of syntax in old scripts

從PHP文件:http://php.net/manual/en/language.types.array.php

編輯有人指出,在呼應得使用單引號,這是一個問題了。使用單引號,PHP不會解釋字符串的內容,而使用雙引號,PHP將解析它,並正確使用這些值。

+0

我明白你在說什麼,但你是什麼建議沒有工作。 – HoppedUpDesigns

+0

我只是在想 - 沒有MySQL只使用**小寫的表名和列名?嘗試使用'birdid'而不是'birdID',等等。可悲的是,我現在沒有MySQL來測試它。 另外,嘗試在MySQL工作臺或PHPMyAdmin中運行查詢本身 - 無論你有什麼... – ppeterka

+0

我現在有正確的數據顯示,但排序不工作 – HoppedUpDesigns

相關問題