-1

I have a simple query ending like this...

group by subject
order by count_entries DESC, person ASC 
LIMIT 0,10

It's not at all taxing (runs immediately/correctly when I run it directly in my MySQL database using Sequel Pro).

But when run via a PHP file it returns a 500 error. All I have to do to remove the 500 error is kill the reference to DESC (either remove completely or replace with ASC) - at which point I get my results from the DB.

I really need the results sorted in descending order though, and am completely out of ideas on this really bizarre problem.

Has anyone ever seen similar behaviour with the DESC keyword?! Does anyone have any ideas at all about what I could try to fix it?!

Thanks for any thoughts.

EDIT - full code as requested...

<?php
$dbhost = 'myIP'; 
$dbuser = 'myUser'; 
$dbpass = 'myPassword'; 
$conn = mysql_connect ($dbhost, $dbuser, $dbpass) or die ('error connecting to your database'); 
$dbname = 'myDBName'; 
mysql_select_db($dbname); // connects to your database
$myquery = "select s.subject_id
, count(e.entry_id) count_entries
, e.person
from subjects s, entries e
where s.subject_id = e.subject_id
group by s.subject_id 
order by count_entries DESC, e.person ASC
LIMIT 0,10";

$query = mysql_query($myquery);

    if ( ! $myquery ) {
        echo mysql_error();
        die;
    }
    $data = array();

    for ($x = 0; $x < mysql_num_rows($query); $x++) {
        $data[] = mysql_fetch_assoc($query);
    }

    echo json_encode($data, JSON_PRETTY_PRINT);  
    mysql_close($conn);
?>
d3wannabe
  • 1,207
  • 2
  • 19
  • 39
  • Hi Dagon, I've amended my original post as requested – d3wannabe Jul 18 '15 at 11:26
  • P.s. I get the exact same problem if I even remove the e.person from the select and the e.person ASC from the order by. The only thing that seems to effect success/failure is the presence of the DESC keyword – d3wannabe Jul 18 '15 at 11:27
  • `Order by` makes no sense this can only return one row due to the use of the aggrigate count –  Jul 18 '15 at 11:34
  • No there's lots of rows - there's a 1-many relationship between subjects and entries. E.g. 10 subject_ids come back, each of which has a count_entries against it - and I need to ensure the count_entries is ordered from biggest to smallest. – d3wannabe Jul 18 '15 at 11:38

1 Answers1

1

Put the following at the beginning of your code for the error to be displayed instead of the vague 500 error.

ini_set('display_errors', 1);

(Taken from: 500 Internal Server Error for php file not for html)

Community
  • 1
  • 1
dash_a
  • 61
  • 2
  • That just fixed the entire problem! Now the DESC keyword is working again. Sorry all - if I was reading this myself I'd have a hard time believing there wasn't something being omitted, but that really is all that was required to fix it!!! – d3wannabe Jul 18 '15 at 11:45