DiegoP.
DiegoP.

Reputation: 45757

mySQL Order By String

I am using the following function to SELECT and ORDER some images from my DB, but the ORDER does not work.

$qry="SELECT * FROM modzzz_articles_screenshots WHERE entry_id='".$aDataEntry['id']."'";
$qryr=mysql_query($qry) or die("Error selecting: ".mysql_error());

while($qryrow = mysql_fetch_array($qryr)) {

   $media_book_id = $qryrow['media_id'];
   $qry2="SELECT * FROM bx_photos_main WHERE ID='".$media_book_id."' ORDER BY Title DESC";
   $qryr2=mysql_query($qry2) or die("Error selecting: ".mysql_error());

   while($qryrow2 = mysql_fetch_array($qryr2)) {

        $photo_book_id = $qryrow2['ID'];
        $a = array ('ID' => $aAuthor['ID'], 'Avatar' => $photo_book_id);
        $aMedia_book_icon = BxDolService::call('photos', 'get_image', array($a, 'file'), 'Search');
        $aMedia_book_iconUrl = $aMedia_book_icon['file'];
        $sRet .='<a class="fancybox" rel="gallery" href="'.$aMedia_book_iconUrl.'" title=""><img src="'.$aMedia_book_iconUrl.'"></a>';

   }
}

ORDER BY title ASC or DESC always returns the same thing.

Is there any error?

Could the problem be that the Title column is not an INT value but VARCHAR?

If that's the problem, I cannot change the column Title, is there an easy way to order the result anyway?

Thanks

Upvotes: 1

Views: 2257

Answers (1)

Gabriel Santos
Gabriel Santos

Reputation: 4974

Here you limit only one record:

   $qry2="SELECT * FROM bx_photos_main WHERE ID='".$media_book_id."' ORDER BY Title DESC";

So, order ASC or DESC will be the same ever. Supposing $media_book_id = 1;

SELECT * FROM bx_photos_main WHERE ID='".$media_book_id."' ORDER BY Title DESC

or

SELECT * FROM bx_photos_main WHERE ID='".$media_book_id."' ORDER BY Title DESC

Will output EVER (with your table structure)

id | title | file
-----------------------
01 | Test  | test.jpg

Because I have not another line to order ascending or descending.

If I have two lines, it will output for ASC:

id | title | file
-----------------------
01 | bar   | test.jpg
02 | foo   | test.jpg

And for DESC:

id | title | file
-----------------------
01 | foo   | test.jpg
02 | bar   | test.jpg

Upvotes: 2

Related Questions