#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    85
    Rep Power
    2

    Sort then display database query issue


    I am getting the error "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY serverUptimeDESC' at line 1"

    I am not sure what would be occurring this, or at this point, how to fix it...

    Here is my HTML Form:
    Code:
    <form id="listSearch" align="center" action="">
                <select id="sortBy" selected="serverUptime" name="sortBy">
                    <option value="serverStatus">Status</option>
                    <option value="serverUptime" selected>Uptime</option>
                    <option value="serverVotes">Votes</option>
                    <option value="serverDateAdded">Existance</option>
                </select>
                <select id="orderBy" selected="DESC" name="orderBy">
                    <option value="DESC" selected>Descending</option>
                    <option value="ASC">Ascending</option>
                </select>
                <button type="submit" class="btn" style="margin-top: -10px;">Search the List!</button>
            </form>
    My function (this is included on the page):
    PHP Code:
    function getServersNormal() {
        if(isset(
    $_GET['pagenumber'])){
            
    $pagenumber $_GET['pagenumber'];
            } else {
            
    $pagenumber 1;
        }
        
    $serversPerPage 10;
        
    $firstPost = ($pagenumber 1) * $serversPerPage;

        if(isset(
    $_GET['sortBy'])) {
            if(isset(
    $_GET['orderBy'])) {
                
    $sortBy $_GET['sortBy'];
                
    $orderBy $_GET['orderBy'];
                
    $query mysql_query("SELECT * FROM servers LIMIT " $firstPost "," $serversPerPage " ORDER BY " $sortBy ", "$orderBy) or die(mysql_error());
            } else {
                
    $query mysql_query("SELECT * FROM servers LIMIT " $firstPost "," $serversPerPage " ORDER BY " $sortBy) or die(mysql_error());
            }
        } else {
            
    $query mysql_query("SELECT * FROM servers LIMIT " $firstPost "," $serversPerPage) or die(mysql_error());
        }
        while(
    $row mysql_fetch_assoc($query)) {
            
    $id $row['ID'];
                    
    $file "banners/".$id.".png";
                    
    $option = (file_exists($file) ? "<img class=\"server_img\" src=\"./banners/" $row['ID'] . ".png" "\" />" "<img class=\"server_img\" src=\"./banners/blank.png\" />");
            if (
    $row['serverStatus'] == 1) {
                        echo 
    "<tr class=\"success\">" "<td class=\"table-name\">" "<a href=\"viewServer.php?id=" $row['ID'] . "\">" $option "</br>" "</a>" "<span class=\"label label-success no-link\">" $row['serverVersion'] . "</span> " "<a href=\"viewServer.php?id=" $row['ID'] . "\">" $row['serverName'] . "<span>" " &#x2022 " "</span>" $row['serverIP'] . "</a>" "</td>";
                        echo 
    "<td class=\"table-players\">" $row['serverPlayersOnline'] . " / " $row['serverPlayersTotal'] . "</td>";
                        echo 
    "<td class=\"table-uptime\">" $row['serverUptime'] . "%" "</td>";
                        echo 
    "<td class=\"table-votes\">" $row['serverVotes'] . "</td>" "</tr>";
                    } else {
                        echo 
    "<tr class=\"error\">" "<td class=\"table-name\">" "<a href=\"viewServer.php?id=" $row['ID'] . "\">" $option "</br>" "<span class=\"label label-success no-link\">" $row['serverVersion'] . "</span> " "<a href=\"viewServer.php?id=" $row['ID'] . "\">" $row['serverName'] . "<span>" " &#x2022 " "</span>" $row['serverIP'] . "</a>" "</td>";
                        echo 
    "<td class=\"table-players\">" $row['serverPlayersOnline'] . " / " $row['serverPlayersTotal'] . "</td>";
                        echo 
    "<td class=\"table-uptime\">" $row['serverUptime'] . "%" "</td>";
                        echo 
    "<td class=\"table-votes\">" $row['serverVotes'] . "</td>" "</tr>";
                    }
        }

    Thanks.

    EDIT: Now that I look at it, I also need everything sorted before it gets listed in the table format.
    Last edited by tjswebdev; March 18th, 2013 at 07:53 PM.
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,997
    Rep Power
    9397
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    Sydney Australia
    Posts
    183
    Rep Power
    83
    You html form is a bit "wrong"
    Try this.
    Code:
    <form id="listSearch" action="">
                <select id="sortBy" name="sortBy">
                    <option value="serverStatus">Status</option>
                    <option value="serverUptime" selected="selected">Uptime</option>
                    <option value="serverVotes">Votes</option>
                    <option value="serverDateAdded">Existance</option>
                </select>
                <select id="orderBy" name="orderBy">
                    <option value="DESC" selected="selected">Descending</option>
                    <option value="ASC">Ascending</option>
                </select>
                <button type="submit" class="btn" style="margin-top: -10px;">Search the List!</button>
            </form>
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    85
    Rep Power
    2
    Originally Posted by requinix
    ORDER BY comes before LIMIT.
    So it should be this?
    PHP Code:
    $query mysql_query("SELECT * FROM servers ORDER BY " $sortBy ", "$orderBy " LIMIT " $firstPost "," $serversPerPage . ) or die(mysql_error()); 
    And also, will it group each set of data, like what would go with the ID? So random etchings would not be scattered?

    And thanks BarryG, I will fix it!
    Last edited by tjswebdev; March 18th, 2013 at 09:35 PM.
  8. #5
  9. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,997
    Rep Power
    9397
    Originally Posted by tjswebdev
    And also, will it group each set of data, like what would go with the ID? So random etchings would not be scattered?
    I don't know what you're saying but the answer is probably "no".
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    85
    Rep Power
    2
    Originally Posted by requinix
    I don't know what you're saying but the answer is probably "no".
    Hmm... Okay... Let me reword that. So in my database for example i have ID, serverName, uptime, etc... Now when I use this will the row of data be sorted and stay with the other records of row ID.

    I hope that made sense. And if they don't stay together, is there a way to do so?
  12. #7
  13. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,997
    Rep Power
    9397
    It will sort by whatever you tell it to sort by. It won't group things together unless they happen to sort that way.

    Now, what does "stay together" mean?
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    85
    Rep Power
    2
    I fixed it.
    Last edited by tjswebdev; March 19th, 2013 at 03:39 PM.

IMN logo majestic logo threadwatch logo seochat tools logo