ryuutatsuo
ryuutatsuo

Reputation: 3996

sql query not working with order by

Here is my original query that works

 Select * FROM story st, sentences s, speaker sp 
 WHERE (st.lesson_id = '1' AND 
        st.speaker_id = sp.speaker_id AND 
        st.sentence_id = s.sentence_id)

When I try to add a Order By it breaks down.

 Select * FROM story st, sentences s, speaker sp 
 WHERE (st.lesson_id = '1' AND 
        st.speaker_id = sp.speaker_id AND 
        st.sentence_id = s.sentence_id) ORDER BY st.story_in_lesson_id ASC

Can't figure out why it is breaking.

EDIT: Here is the error I get Fatal error: Call to a member function fetch_object()

EDIT: My PHP code

$result = $mysqli->query("Select * FROM story st, sentences s, speaker sp 
                         WHERE st.lesson_id = '1' AND 
                         st.speaker_id = sp.speaker_id AND 
                         st.sentence_id = s.sentence_id 
                         ORDER BY st.story_in_lesson_id ASC");

while ($value = $result->fetch_object()) { 
    //never goes in here fails at the fetch_object()
}

EDIT:

Is it possible that it does not work because I am trying to query multiple tables? Only one of the tables has the story_in_lesson_id which is the story table. When I run the query just on that table it works find.

EDIT:

More info, copied the DB over to my work Mac and the query works!!! But why does it not work on my computer?? By the way I am strictly testing the query now by just running it inside Sequel Pro.

Upvotes: 3

Views: 1060

Answers (4)

ryuutatsuo
ryuutatsuo

Reputation: 3996

Thank you for all the help! I have solved the issue!!!

In the end it ended up being a file writing permissions issue. I figured it out by running my query in Sequel Pro which threw a error about some file located at /var/folders/41/some big number/T/ . I went to the dir and allowed for write access tried the query in Sequel Pro and it worked. Then I go to my php and try it there and it worked.

If anyone can answer why this happened? Do all queries write to a log file or anything like that? If anyone can share some more inside.

Upvotes: 0

Chirag Pipariya
Chirag Pipariya

Reputation: 441

Please remove your where condition dear ..... may be it's working and please give me reply what you getting result .

SELECT * 
FROM   story st 
       INNER JOIN sentences s 
               ON st.sentence_id = s.sentence_id 
       INNER JOIN speaker sp 
               ON st.speaker_id = sp.speaker_id 
ORDER  BY st.story_in_lesson_id ASC 

Upvotes: 3

Rohan Kumar
Rohan Kumar

Reputation: 40639

You have to check st.story_in_lesson_id exists in your story table and to prevent from Fatal error: Call to a member function fetch_object() you should try it like,

$query = "Select * FROM story st, sentences s, speaker sp 
              WHERE st.lesson_id = '1' AND 
              st.speaker_id = sp.speaker_id AND 
              st.sentence_id = s.sentence_id 
              ORDER BY st.story_in_lesson_id ASC";
              // check story_in_lesson_id exists in story table 

if ($result = $mysqli->query($query)) {
    while ($value = $result->fetch_object()) { 
       // while code
    } // while ends
} // if result ends

Upvotes: 1

Saharsh Shah
Saharsh Shah

Reputation: 29051

Try this:

SELECT * 
FROM story st
INNER JOIN sentences s ON st.sentence_id = s.sentence_id
INNER JOIN speaker sp ON st.speaker_id = sp.speaker_id
WHERE st.lesson_id = '1' 
ORDER BY st.story_in_lesson_id ASC

Upvotes: 0

Related Questions