【问题标题】:as I can sort the results of a query to my sql database?因为我可以将查询结果排序到我的 sql 数据库?
【发布时间】:2013-02-27 13:21:21
【问题描述】:

我需要你帮助我

我尝试了几种方法,我得到了错误

排序希望如下: 速率 DESC 年龄ASC 高度 DESC 体重ASC

也想按字段排序

如果你能用一个例子来解释,那将是完美的注意顺序是如何以及在哪里的

                   <? Php

                   $ O ='';
                   / / Put the appropriate information:
                   $ Data = array ('localhost', 'user', 'password');
                   $ Con = mysql_connect ($ data [0], $ data [1], $ data [2]);

                   if ($ con) {
                   $ O = 'Error: Could not connect to server. '. mysql_error ();
                   echo $ o;
                   exit;
                   }

                   / / Change the name of the database for yours
                   $ Db_name = 'database';

                   if (mysql_select_db ($ db_name, $ con)) {
                   $ O = 'Error: Could not select database "'. $ Db_name. '". '.                        mysql_error ();
                   echo $ o;
                   exit;
                   }

                   $ Table = 'users' / / Change this only if you know what it does.
                   $ Query = "SELECT * FROM $ table";
                   $ Where = "WHERE";
                   $ And = 0;


                   if (isset ($ _GET ['name']) &&! empty ($ _GET ['name'])) {
                   $ Where. = "Name LIKE '% $ _GET [name]%'";
                   $ And = 1;
                   }

                   if (isset ($ _GET ['rate'])) {
                   $ E = explode ('-', $ _GET ['rate']);

                   if (is_numeric ($ e [0]) && is_numeric ($ e [1])) {
                   if ($ and === 1)
                   $ Where. = "AND";


                   $ Where. = "Rate BETWEEN $ e [0] AND $ e [1]";
                   $ And = 1;
                   }
                   }

                   if (isset ($ _GET ['age'])) {
                   $ E = explode ('-', $ _GET ['age']);

                   if (is_numeric ($ e [0]) && is_numeric ($ e [1])) {
                   if ($ and === 1)
                   $ Where. = "AND";


                   $ Where. = "Age BETWEEN $ e [0] AND $ e [1]";
                   $ And = 1;
                   }
                   }

                   if (isset ($ _GET ['Height'])) {
                   $ E = explode ('-', $ _GET ['Height']);

                   if (is_numeric ($ e [0]) && is_numeric ($ e [1])) {
                   if ($ and === 1)
                   $ Where. = "AND";


                   $ Where. = "Height BETWEEN $ e [0] AND $ e [1]";
                   $ And = 1;
                   }
                   }

                   if (isset ($ _GET ['weight'])) {
                   $ E = explode ('-', $ _GET ['weight']);

                   if (is_numeric ($ e [0]) && is_numeric ($ e [1])) {
                   if ($ and === 1)
                   $ Where. = "AND";


                   $ Where. = "Weight BETWEEN $ e [0] AND $ e [1]";
                   $ And = 1;
                   }
                   }

                   if (isset ($ _GET ['City']) &&! empty ($ _GET ['city'])) {
                   if ($ and === 1)
                   $ Where. = "AND";

                   $ Where. = "City = '$ _GET [City]'";
                   $ And = 1;
                   }

                   if (isset ($ _GET ['Eyes']) &&! empty ($ _GET ['Eyes'])) {
                   if ($ and === 1)
                   $ Where. = "AND";

                   $ Where. = "Eyes = '$ _GET [Eyes]'";
                   $ And = 1;
                   }

                   if (isset ($ _GET ['Hair']) &&! empty ($ _GET ['Hair'])) {
                   if ($ and === 1)
                   $ Where. = "AND";

                   $ Where. = "Hair = '$ _GET [Hair]'";
                   $ And = 1;
                   }




                   if (strlen ($ where)> 6)
                   $ Query. = $ Where;

                   $ Result = mysql_query ($ query, $ con);



                   if ($ result) {
                   $ Nrows = mysql_num_rows ($ result);

                   if ($ nrows> 0) {
                   $ O ='';

                   while ($ row = mysql_fetch_assoc ($ result)) {

                   $ O. = "$ Row [image]";
                   }

                   $ O. = "";
                   Else {}
                   $ O = 'There were no results';
                   }
                   Else {}
                   $ O = 'Error: Unable to run the query. '. mysql_error ($ con);
                   }

                   mysql_free_result ($ result);
                   mysql_close ($ con);
                   echo $ o. "";
                   exit;
                   ?>

【问题讨论】:

  • 你遇到了什么错误?
  • 如果您没有收到任何错误:请给我们完整的查询 (echo $query)
  • 关于$Where。有一个巨大的错误:$Where .= "AND"; $Where .= "Hair = ..."。必须有空格!
  • $ Query. = $ Where; 之后。您只需输入$ Query. = "ORDER BY rate DESC, age ASC, Height DESC, weight ASC"。大手笔……
  • $ variblename 是语法错误...

标签: php mysql sql


【解决方案1】:

在尝试修复 SQL 错误之前,请修复 PHP 错误。例如; $ O 不正确,应该是 $O$_GET[]。 此外,PHP 区分大小写,因此当您的意思是 $O 时,您不能使用 $o

关于 SQL,正如 Benjamin 指出的,您需要在 SQL 关键字的前后放置空格,$ Query. = $ Where; 可能应该是 $Query. = " ".$Where;

此外,由于用户的权限取决于他们正在访问的数据库,因此您应该在提供用户详细信息之前选择数据库。 (或同时)。

对于复杂的查询,请尝试使用 MySQL shell。一旦你弄清楚了查询,然后把它交给 PHP 端。至少我是这样做的。

祝你好运,小睡

【讨论】:

    猜你喜欢
    • 2010-09-11
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多