2014-11-23 88 views
0

我有一個按ASC順序排列名稱的表,但是當我單擊按鈕時它不起作用。 我試着用2個按鈕做同樣的事情,並檢查了一些可用的代碼,但它根本不起作用。任何幫助?使用PHP排序MYSQL表格

PHP代碼:

<?php 
    $servername = "localhost"; 
    $username = "root"; 
    $password = ""; 
    $dbname = "myfeeds"; 

    $conn = mysql_connect($servername, $username, $password, $dbname); 
    if (!$conn) { 
     die("Connection failed"); 
    } 

    $db = mysql_select_db("myfeeds", $conn); 
    if (!$db) { 
     die("Can't select database"); 
    } 

    if (isset($_POST['asc'])) { 
     $result = mysql_query("SELECT * FROM websites ORDER BY name ASC"); 
    } else { 
     $result = mysql_query("SELECT * FROM websites ORDER BY name DESC"); 

    } 

    if (!$result) { 
     die("Failed to show queries from table"); 
    } 


    $num = mysql_numrows($result); 
    mysql_close(); 
    ?> 

這裏的按鈕:

SORT BY: 
      <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
       <button type="submit" id="asc" name="asc">ASC</button> 
      </form> 

表:

    <table cellpadding="0"> 
        <tr> 
         <th>ID</th> 
         <th>Name</th> 
         <th>URL</th> 
         <th>Description</th> 
         <th>Logo</th> 
        </tr> 

        <?php 
        $i = 0; 

        while ($i < $num) { 
         $f5 = mysql_result($result, $i, "id"); 
         $f1 = mysql_result($result, $i, "name"); 
         $f2 = mysql_result($result, $i, "url"); 
         $f3 = mysql_result($result, $i, "description"); 
         $f4 = mysql_result($result, $i, "image"); 
         ?> 
         <tr> 
          <td><?php echo $f5; ?></td> 
          <td><?php echo $f1; ?></td> 
          <td><?php echo $f2; ?></td> 
          <td><?php echo $f3; ?></td> 
          <td><?php echo "<img src='$f4'>"; ?></td> 
         </tr> 
         <?php 
         $i++; 
        } 
        ?> 
       </table> 
+1

旁註:'mysql_numrows'無效。這應該讀作'mysql_num_rows'。另外,你在哪裏迴應你的結果?這不是你發佈的內容。你需要回應這些。 – 2014-11-23 04:27:16

+0

在打開'<?php'標記後立即在文件頂部添加錯誤報告 'error_reporting(E_ALL); ini_set('display_errors',1);'看看它是否產生任何東西。還有'或者(mysql_error())'去''mysql_query()'。做'死(「消息X」)'沒有幫助。 – 2014-11-23 04:29:06

+0

嘗試'' – cepradeep 2014-11-23 04:32:15

回答

1

試試這個..工作100%=)

<?php 
    $servername = "localhost"; 
    $username = "root"; 
    $password = ""; 
    $dbname = "myfeeds"; 

    $conn = mysql_connect($servername, $username, $password, $dbname); 
    if (!$conn) { 
     die("Connection failed"); 
    } 

    $db = mysql_select_db("myfeeds", $conn); 
    if (!$db) { 
     die("Can't select database"); 
    } 

    if (isset($_GET['asc'])) 
     $result = mysql_query("SELECT * FROM websites ORDER BY name ASC"); 
    else 
     $result = mysql_query("SELECT * FROM websites ORDER BY name DESC"); 


    if (!$result) 
     die("Failed to show queries from table"); 

    if (mysql_num_rows($result) > 0) { 
     // output data of each row 
     while($row = mysql_fetch_assoc($result)) { 
      echo "name: " . $row["name"]. "<br>"; 
     } 
    } else { 
     echo "0 results"; 
    } 

    $num = mysql_numrows($result); 
    mysql_close(); 

?> 

<form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
    <button type="submit" id="asc" name="asc" value="asc">ASC</button> 
    <button type="submit" id="asc" name="desc" value="desc">DESC</button> 
</form> 

它是更好地使用PDO這是從數據庫

<?php 
    $servername = "localhost"; 
    $username = "root"; 
    $password = ""; 
    $dbname = "myfeeds"; 


    try 
    { 
     $conn = new PDO("mysql:host=".$servername.";dbname=".$dbname, $username, $password); 
     $conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); 
     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    } 
    catch (PDOException $e) 
    { 
     die($e->getMessage()); 
    } 


    try 
    { 
     if (isset($_GET['asc'])) 
      $result = $conn->prepare("SELECT * FROM websites ORDER BY name ASC"); 
     else 
      $result = $conn->prepare("SELECT * FROM websites ORDER BY name DESC"); 

     $result->execute(); 

     if($result->rowCount()) 
     { 
      while($r = $result->fetch(PDO::FETCH_OBJ)) 
      { 
       echo 'Name:' . $r->name . '<br/>'; 
      } 
     } 
     else echo 'no record found!'; 

    } 
    catch (PDOException $e) 
    { 
     die($e->getMessage()); 
    } 

?> 

查看DATAS如何..用mysql

<table cellpadding="0"> 
    <tr> 
     <th>ID</th> 
     <th>Name</th> 
     <th>URL</th> 
     <th>Description</th> 
     <th>Logo</th> 
    </tr> 

    <?php 

    while ($row = mysql_fetch_assoc($result)) { 
     ?> 
     <tr> 
      <td><?php echo $row["id"]; ?></td> 
      <td><?php echo $row["name"]; ?></td> 
      <td><?php echo $row["url"]; ?></td> 
      <td><?php echo $row["description"]; ?></td> 
      <td><?php echo "<img src='".$row["image"]."'>"; ?></td> 
     </tr> 
     <?php 

    } 
    ?> 
</table> 
+0

你測試過了嗎? – 2014-11-23 04:53:48

+0

這是肯定的工作..因爲我在發佈前測試 – jmn 2014-11-23 05:05:48

+0

@jmn我試過你的代碼,它的工作原理,但我不知道如何使用我的表輸出實現它。 – Kamilah 2014-11-23 06:31:35

2

按照manualmysql_connect第四個參數應該是一個新的鏈接連接,而不是數據庫名稱。

new_link

如果再次調用mysql_connect()使用相同的參數製作,沒有新的鏈接將被建立,而是已經打開的鏈接的鏈接標識將被退回。 >new_link參數修改此行爲並使mysql_connect()始終打開一個新鏈接,即使之前使用相同參數調用mysql_connect()。在SQL安全模式下,該參數被忽略。

我建議使用mysqli_*來代替,因爲mysql已被棄用。

當然,不要忘記在查詢後獲取行。

$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "myfeeds"; 

$conn = mysqli_connect($servername, $username, $password, $dbname); 

$order = isset($_POST['asc']) ? 'ASC' : 'DESC'; 
$sql = "SELECT * FROM websites ORDER BY name $order"; 
$query = mysqli_query($conn, $sql); 

$num = $query->num_rows; 
if($num > 0) { 
    while($row = mysqli_fetch_assoc($query)) { 
     echo $row['name'] . '<br/>'; 
    } 
} 
+1

*「根據手冊,mysql_connect的第四個參數應該是一個新的連接鏈接,而不是數據庫名稱。」* - 好吧,我覺得像一個真正的「duh」大聲笑+1;)只是去顯示多少'我知道。我是一個「mysqli/PDO」傢伙。 – 2014-11-23 04:49:23

+0

@ Fred-ii-是的,我以爲我一開始就看到mysqli,但它是mysql_connect,並且在那裏有'$ dbname'有一點點關閉。 – Ghost 2014-11-23 04:51:38

+0

使用'死亡(「連接失敗」)的OP也沒有幫助。 '死(mysql_error())'更好。 – 2014-11-23 04:52:33