2013-02-27 23 views
-3

我需要你帮我因为我可以将查询的结果排序到我的sql数据库?

我试过几种方法,我得到错误

排序要如下: 率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; 
        ?> 
+0

你会得到什么错误? – 2013-02-27 13:22:40

+0

如果你完全没有错误:请给我们整个查询('echo $ query') – 2013-02-27 13:23:18

+0

WTF'$ Where'? – Winston 2013-02-27 13:24:06

回答

0

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

在问候的SQL,本杰明指出的那样,你需要把空格的前面,SQL关键字和$ Query. = $ Where;后大概应该是$Query. = " ".$Where;

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

对于复杂的查询,请尝试使用MySQL shell。一旦你找出查询,然后把它转移到PHP端。至少我是这么做的。

祝你好运,Nap

相关问题