sql query not working with order by

◇◆丶佛笑我妖孽 提交于 2019-12-07 18:01:37

问题


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.


回答1:


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 



回答2:


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



回答3:


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



回答4:


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.



来源:https://stackoverflow.com/questions/20603631/sql-query-not-working-with-order-by

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!