0

I've tried to give my "get_threads" variable some additional content, based on the actual case. But it doesn't work as expected, the query isn't executing at all. It looks like there are some blanks that are missing in the "final" variable. But when i add them to the value, the output completely dissapears.

The output query is:

SELECT id, main_forum_id, icon_id, title, description, author_id, closed, views, posts, date_created, last_post_author_id, last_replyTime FROM forum_thread WHERE main_forum_id= ('1') ORDER BY views ASC LIMIT 0, 20

And that's the code:

$get_threads  = "SELECT id, main_forum_id, icon_id, title, description, author_id, closed, views, posts, date_created, last_post_author_id, last_replyTime FROM forum_thread WHERE main_forum_id= ('" . $actualBoard . "')";
if (isset($_GET[ 'sortField' ])) {
switch ($_GET[ 'sortField' ]) {
    case topic:
        $get_threads  .= " ORDER BY title ASC ";
        break;

    case rating:
        $get_threads  .= " ORDER BY rating ASC ";
        break;

    case replies:
        $get_threads  .= " ORDER BY replies ASC ";
        break;

    case views:
        $get_threads  .= " ORDER BY views ASC ";
        break;

    case lastReply:
        $get_threads  .= " ORDER BY last_replyTime DESC ";
        break;      
        }
    } else {
$lastReplyClass = 'columnLastPost active';
$get_threads  .= " ORDER BY last_replyTime ASC ";
    }
$get_threads  .= " LIMIT $start, $perPage";

SOLUTION:

Okay, i'm such an idiot.. Had a format function for the time and deleted it. That caused the error:

Fatal error: Call to undefined function formatDateString() 
3
  • Try to put a white space after all your "ORDER BY [...] ASC|DESC " Commented Apr 28, 2014 at 10:02
  • insert quotes in case parameters. example: case 'topic': (if topic not defined as constant) Commented Apr 28, 2014 at 10:35
  • and you can insert default case in switch... Commented Apr 28, 2014 at 10:40

3 Answers 3

1
SELECT id, main_forum_id, icon_id, title, description, author_id, closed, views, posts, date_created, last_post_author_id, last_replyTime FROM forum_thread WHERE main_forum_id= ('1') ORDER BY last_replyTime ASCLIMIT 20, 20

ASCLIMIT ?

There has to be a space.

You should add a space before your LIMIT and before your ORDER BY

$get_threads  .= " ORDER BY last_replyTime ASC";
    }
$get_threads  .= " LIMIT $start, $perPage";
Sign up to request clarification or add additional context in comments.

7 Comments

I know, but that's the problem. If i add a space, the output is: nothing. No PHP error, no MySql error. Just nothing.
Try to use the query in the database and try if it works, thats the first step in debugging SQL =)
the output cant be nothing, only because you add a space. Then you add the space in the wrong place or something oo. A space doesn´t kill anything.
Edited code you can see on the top. I am REALLY sure, that my code is correct. Before i splitted the query into this parts, it worked like a charm. And it's just all about that damn spaces.
Your still missing the space before the LIMIT, it still says ASCLIMIT instead of ASC LIMIT
|
0
$get_threads  = "SELECT id, main_forum_id, icon_id, title, description, author_id, closed, views, posts, date_created, last_post_author_id, last_replyTime FROM forum_thread WHERE main_forum_id= ('" . $actualBoard . "') ";
    $sortField = (isset($_GET[ 'sortField' ])) ? $_GET[ 'sortField' ] : '';

    switch ($sortField) {
        case 'topic':
            $get_threads  .= " ORDER BY title ASC ";
            break;

        case 'rating':
            $get_threads  .= " ORDER BY rating ASC ";
            break;

        case 'replies':
            $get_threads  .= " ORDER BY replies ASC ";
            break;

        case 'views':
            $get_threads  .= " ORDER BY views ASC ";
            break;

        case 'lastReply':
            $get_threads  .= " ORDER BY last_replyTime DESC ";
            break;      
        default:{
            $lastReplyClass = 'columnLastPost active';
            $get_threads  .= " ORDER BY last_replyTime ASC ";
            }
            break;      
    }

    $get_threads  .= " LIMIT $start, $perPage";

Comments

0

Give the space between ORDER BY last_replyTime ASC and LIMIT 20 20

Also switch case surrounded with single(') or double("") quotes.

For example,

case "topic":
 break;
case "views":
 break;

6 Comments

That's the point of this question...When i add the spaces correctly, the output is exaclty nothing.
@RedDragon55 : Before executing, just echo your query, and run on DB. Check if you got any results? Also update your question with your query.
@RedDragon55 : You got any results when running the query? I want that query alone. For example SELECT ... WHERE main_forum_id= ('1') ... LIMIT 20, 20 not your functional part.
Yes, the results i expect. I already posted the query. SELECT id, main_forum_id, icon_id, title, description, author_id, closed, views, posts, date_created, last_post_author_id, last_replyTime FROM forum_thread WHERE main_forum_id= ('1') ORDER BY views ASC LIMIT 0, 20
@RedDragon55 : You forgot the quotes around the switch case string values. I updated my answer now.
|

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.