2016-05-08 51 views
0

此腳本允許用戶從下拉列表中選擇一個表格,然後顯示所選表格的內容。下拉表格以顯示內容

  1. 此時數據庫連接正在工作。
  2. 表格列表顯示在下拉列表中。

問題:沒有顯示數據庫表中的內容。 我已經通過代碼檢查,一切都看起來不錯,但它似乎仍然只是部分工作。

<?php 
//update this to your DB connection details. 
$dbh = "localhost"; 
$dbn = "dbname"; 
$dbu = "dbuser"; 
$dbp = "dbpass"; 

$conn = mysql_connect($dbh,$dbu,$dbp) or die("Unable to connect do database."); 
mysql_select_db($dbn, $conn) or die("Unable to select database."); 

//Some vars for Order by and Limit. 
if (!isset($ordBy)){ 
    $ordBy = 1; 
} 
if (!isset($ps)){ 
    $ps = 0; 
} 
if (!isset($ord)){ 
    $ord = 1; 
} 
if ($ord == 1){ 
    $tOrder = "ASC"; 
} else { 
    $tOrder = "DESC"; 
} 

//Tables drop-down 

$result = mysql_query("SHOW TABLES FROM $dbn") or die("Cannot list table names."); 
echo " 
<form name=\"table_browser\" action=\"".$PHP_SELF."\" method=\"GET\" > 
    <select name=\"t\" onChange=\"javascript:submit();\"> 
     <option>Select a table</option> 
"; 
while ($row = mysql_fetch_row($result)){ 
    echo "  <option value=".$row[0].">".$row[0]."</option>\n"; 
} 
echo " </select> 
</form>\n"; 

if (!isset($t)){ 
    die("Please select a table"); 
} 

//Get number of rows in $t and then select 
$result = mysql_query("SELECT COUNT(*) FROM $t") or die("The requested table doesn't exist."); 
$total = mysql_result($result,0); 
$qry = "SELECT * FROM $t ORDER BY ".$ordBy." ".$tOrder." LIMIT ".($ps*20).",20 "; 

if (isset($qry)) { 
    $result = mysql_query($qry) or die("The requested table doesn't exist."); 
    $i = 0; 
    while ($i < mysql_num_fields($result)) { 
     $meta = mysql_fetch_field($result); 
     if (!$meta) { 
      echo "No information available on the table<br />\n"; 
     } 
     $name[$i] = $meta->name; 
     $i++; 
    } 
    //Display table details 
    echo "Rows ".($ps*20+1)." to ".((($ps+1)*20 < $total) ? (($ps+1)*20) : ($total))." of $total from table: 

<b>$meta->table</b>\n<br /><br />\n"; 

    //Count results 
    if ($ps > 0) { 
     echo "<a href=\"browse.php?t=$t&ps=".($ps-1)."&ordBy=$ordBy&ord=$ord\">20 Previous</a> - "; 
    } else { 
     echo "20 Previous - "; 
    } 
    if ((($ps+1)*20) < $total){ 
     echo "<a href=\"browse.php?t=$t&ps=".($ps+1)."&ordBy=$ordBy&ord=$ord\">Next 20</a>\n"; 
    } else { 
     echo "Next 20\n"; 
    } 

    //Column names 
    echo "<br /><br />\n<table>\n <tr>\n"; 
    for ($j = 0; $j < $i; $j++){ 
     echo "  <td><b><a href=\"browse.php?t=$t&ps=$ps&ordBy=$name[$j]&ord=".(-$ord)."\">$name[$j]</a></b>"; 
     if ($ordBy == $name[$j]) { 

      echo "<img src=\"images/arrow$ord.gif\">"; 
     } 
     echo "</td>\n"; 
    } 
    echo " </tr>\n"; 

    //Data 
    while ($row = mysql_fetch_array($result)){ 
     echo " <tr onmouseover=\"this.style.background='#DDDDDD'\" onmouseout=\"this.style.background=''\">"; 
     for ($j = 0; $j < $i; $j++){ 
      echo "<td>".$row[$name[$j]]."</td>"; 
     } 
     echo "</tr>\n"; 
    } 
    echo "</table>\n"; 
} 
mysql_close(); 
?> 
+0

不清楚你在問什麼,你需要詳細說明。 – Rasclatt

+0

更新問題描述。 – gentlebreeze

回答

0

@gentlebreeze - 我的眼睛試圖讀取傷害 - 但我不能看到你實際上是設置名爲$ t時的變量 - 這是用來確定表。你應該有前行

$t = $_GET['t']; 

地方:

....if (!isset($t)){.... 

,因爲你需要在該行使用它:

....$result = mysql_query("SELECT COUNT(*) FROM $t".... 

,你不應該使用的mysql_query任 - 老現已棄用。你應該切換到PDO和綁定變量。

+0

添加一個變量$ t來測試它的工作。 – gentlebreeze

+0

太棒了 - 很樂意幫忙。 :)) – gavgrif

相關問題