【问题标题】:Sort MYSQL TABLES using PHP使用 PHP 对 MYSQL 表进行排序
【发布时间】:2014-11-23 04:23:25
【问题描述】:

我有一个按 ASC 顺序对名称进行排序的表格,但是当我单击按钮时它不起作用。 我尝试用 2 个按钮做同样的事情,并检查了一些可用的代码,但它根本不起作用。有什么帮助吗?

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>

【问题讨论】:

  • 旁注:mysql_numrows 无效。那应该读作mysql_num_rows。另外,你在哪里回应你的结果?你发的帖子里没有。你需要回应那些。
  • 在打开 &lt;?php 标签 error_reporting(E_ALL); ini_set('display_errors', 1); 后将错误报告添加到文件顶部,看看它是否产生任何结果。还有or die(mysql_error())mysql_query()。做die("Message X") 没有帮助。
  • 试试&lt;input type="submit"&gt;
  • @CEP &lt;button type="submit" id="asc" name="asc"&gt;ASC&lt;/button&gt; 有效。如果 OP 使用了&lt;button type="button" id="asc" name="asc"&gt;ASC&lt;/button&gt;,那么它将无法正常工作。 OP 的错误要么在其他地方并且没有显示,要么如果它是正在使用的确切代码,则对查询没有做任何事情。
  • @Fred-ii- 注意到,谢谢 :) 这对我来说是新的。是的,我认为 OP 并没有对结果做任何事情。

标签: php mysql button sql-order-by


【解决方案1】:

根据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/>';
    }
}

【讨论】:

  • “根据手册,mysql_connect的第四个参数应该是新的连接链接,而不是数据库名。” - 嗯,我感觉像一个真正的“duh”大声笑+1 ;) 只是显示我知道多少mysql_。我是一个“mysqli/PDO”的人。
  • @Fred-ii- 是的,我一开始以为我看到的是 mysqli,但它是 mysql_connect,并且其中有 $dbname 有点不对劲。
  • OP 使用die("Connection failed") 也无济于事。 die(mysql_error()) 更好。
  • @Fred-ii- 令人惊讶的是,在修复了 num 行之后当然是的
  • @Ghost 谢谢你的建议。我是 mysql 和 php 的新手。我只是参考我们讲义中的所有内容。
【解决方案2】:

试试这个.. 工作 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());
    }

?>

使用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>

【讨论】:

  • @jmn 我试过你的代码,它可以工作,但我不知道如何使用我的表输出来实现它。
  • @jmn 怎么发给你?
  • 我已经编辑了代码并添加了如何查看数据库中的信息
猜你喜欢
  • 2012-04-09
  • 2010-10-19
  • 1970-01-01
  • 1970-01-01
  • 2016-10-28
  • 2017-08-20
  • 2016-04-10
  • 2013-09-06
相关资源
最近更新 更多