0

This is my code:

while ($shop = mysql_fetch_array($shop_result)) {
    $item_result = mysql_query("SELECT * FROM db_".$shop['ItemCategory']."s WHERE ItemId = '".$shop['ItemId']."' ORDER BY Level");
    $item = mysql_fetch_assoc($item_result);


echo("<item>");
        echo("<Element>" . $item['ItemElement'] . "</Element>");
        echo("<ItemLevel>" . $item['Level'] . "</ItemLevel>");
echo("</item>");

Here is a typical output:

</items>
<item>
<Element>8</Element>
<Level>15</Level>
</item>
<item>
<Element>4</Element>
<Level>2</Level>
</item>
<item>
<Element>4</Element>
<Level>24</Level>
</item>
<item>
<Element>4</Element>
<Level>17</Level>
</item>
<items>

But the third highest Level is at the top? Why? I ordered by ItemLevel? This is my desired output:

</items>
<item>
<Element>4</Element>
<Level>2</Level>
<item>
<Element>8</Element>
<Level>15</Level>
</item>
<item>
<Element>4</Element>
<Level>17</Level>
</item>
<item>
<Element>4</Element>
<Level>24</Level>
</item>
<items> 

I really cant figure it out. When I run the same thing in Sequel Pro I get what I want, but it's GUI.

3
  • check the value for that specific entry, maybe you have a space or a hidden character or something that is causing the row to be ordered first. check the order shown when they query is done directly in mysql. Commented Jan 8, 2016 at 21:00
  • It's definitely because items come form different loops of while Commented Jan 8, 2016 at 21:01
  • 1
    You're only fetching one row from $item_result. The loop is only for $shop_result, so the ordering is based on that query. Commented Jan 8, 2016 at 21:21

2 Answers 2

1

Level would appear to be a string. So, it is ordering as a string not a number.

Here are two fixes First, order as a number:

order by Level + 0

Second, order by length first:

order by length(Level), Level
Sign up to request clarification or add additional context in comments.

4 Comments

i thought of that too but out of the example then the order should have been 15, 17, 2, 24 but supposedly the LEVEL 15 is the one shown first and 17 at the end.
Another option would be to do: ORDER BY Level * 1
ORDER BY CAST(level AS DECIMAL)
Level is INT in my database. Could it still be a string for some reason?
1

You're not fetching all the rows from $item_result, you're just fetching the first row. You need another while loop.

while ($shop = mysql_fetch_array($shop_result)) {
    $item_result = mysql_query("SELECT * FROM db_".$shop['ItemCategory']."s WHERE ItemId = '".$shop['ItemId']."' ORDER BY Level");
    while ($item = mysql_fetch_assoc($item_result)) {    
        echo("<item>");
                echo("<Element>" . $item['ItemElement'] . "</Element>");
                echo("<ItemLevel>" . $item['Level'] . "</ItemLevel>");
        echo("</item>");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.