0

The code below returns nothing, but if I remove this line:

'desc'          => $row['DESC'],

from the function it works fine.

DESC Is a valid column in the database and when I run the full query in phpmyadmin, it returns the desired result.

I am not sure why this line

'desc'          => $row['DESC'],

breaks the return of the result.

=======

After more investigating I can see the JSON output has the same issue. Altering the column name (since DESC is a keyword) and reflecting the changes in the query has no effect.

========

function get_all_subjects($db1) {
    $stmt = $db1->query("SELECT DISTINCT NAME, DESC, CLASSCODE FROM tbl_subjects WHERE VISIBLE = 1 ORDER BY NAME ASC");
    $stmt->execute();
    $count = $stmt->rowCount();

    $column = array();

    if ($count >0)
    {
        while($row = $stmt->fetch(PDO::FETCH_ASSOC)) 
        {
            $column[] = array(
                        'name'          => $row['NAME'],
                        'desc'          => $row['DESC'],
                        'cc'            => $row['CLASSCODE']
                        );
        }

        return json_encode(array('subjects' =>$column)); 
    }
    else
    {
        return $count;
    }
}
3
  • your query got syntax error, desc is reserved keyword of sql language, use quotes Commented Feb 11, 2015 at 7:29
  • 1
    DESC is a reserved word of MySQL Commented Feb 11, 2015 at 7:30
  • The result (without the line in question) works so I know my jquery works Commented Feb 11, 2015 at 7:45

4 Answers 4

3

DESC is a reserved word in SQL. If you want to use it as a column, you should protect it with forward quotes:

function get_all_subjects($db1) {
    $stmt = $db1->query("SELECT DISTINCT NAME, `DESC` AS D, CLASSCODE FROM tbl_subjects WHERE VISIBLE = 1 ORDER BY NAME ASC");
    $stmt->execute();
    $count = $stmt->rowCount();

    $column = array();

    if ($count >0)
    {
        while($row = $stmt->fetch(PDO::FETCH_ASSOC)) 
        {
            $column[] = array(
                        'name'          => $row['NAME'],
                        'desc'          => $row['D'], // Using the alias, just in case
                        'cc'            => $row['CLASSCODE']
                        );
        }

        return json_encode(array('subjects' =>$column)); 
    }
    else
    {
        return $count;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer and explanation, but I must be having an issue somewhere else. Still no data return. I have also changed the column name to double check.
I marked you as the right answer, since your answer prompted me to look further. I have however posted what the solution to my problem was below.
0

DESC is a reserved Keyword do something like

SELECT DISTINCT NAME, DESC as yourVar, CLASSCODE FROM tbl_subjects WHERE VISIBLE = 1 ORDER BY NAME ASC

and then like that

'desc'          => $row['yourVar'],

Comments

0
$stmt = $db1->query("SELECT DISTINCT `NAME`, `DESC`, `CLASSCODE` FROM `tbl_subjects` WHERE `VISIBLE` = 1 ORDER BY `NAME` ASC");

there was syntax error because desc is reserved keyword, you have to use quotes, the best habbit is quote each column and each table name

Comments

0

Thank you all for your answers - I found the problem and what broke the script was that my strings in my JSON values had several - in them.

It took me a while to find it :-)

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.