Intervelopment Posted November 18, 2009 Share Posted November 18, 2009 Good Morning, I have a bit of an issue with a script im writing and would really appreciate some help. I have 2 tables, the first table has id, question_id, email_address and timestamp. The second table has question_id and question. What i need to be able to do, is get all the question_id's from the first table where the email address is lets say test@test.com Then i need to search through the second table to find a random question with a question_id that is not listed in the results from the first query and print it out. I have been looking around for quite some time but cannot seem to nail it. If anyone could help, that would be greatly appreciated. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/181954-solved-query-help/ Share on other sites More sharing options...
Alex Posted November 18, 2009 Share Posted November 18, 2009 Something like this should do it: $sql = "SELECT question FROM table2 WHERE question_id NOT IN (SELECT question_id FROM table1 WHERE emailaddress = 'test@test.com') ORDER BY RAND() LIMIT 1"; $result = mysql_query($sql); echo $row['question']; Quote Link to comment https://forums.phpfreaks.com/topic/181954-solved-query-help/#findComment-959759 Share on other sites More sharing options...
Intervelopment Posted November 18, 2009 Author Share Posted November 18, 2009 Its not echoing anything out This is what i put. function dispQuestion(){ global $database, $session; $email = $session->usremail; $q = "SELECT question FROM ".TBL_QUESTIONS." WHERE question_id NOT IN (SELECT question_id FROM ".TBL_DONE_QUESTIONS." WHERE email = '$email') ORDER BY RAND() LIMIT 1"; $result = $database->query($q); echo $row['question']; } One thing i did pick up in TBL_QUESTIONS its not question_id ... Its actually id ... but even if i change the question_id right after the WHERE statement to just id .. it still returns nothing Quote Link to comment https://forums.phpfreaks.com/topic/181954-solved-query-help/#findComment-959770 Share on other sites More sharing options...
Alex Posted November 18, 2009 Share Posted November 18, 2009 You're never setting $row, right after this line: $result = $database->query($q); You should have: $row = mysql_fetch_assoc($result); edit: That's my fault for not including it in my original code. :s Quote Link to comment https://forums.phpfreaks.com/topic/181954-solved-query-help/#findComment-959771 Share on other sites More sharing options...
Intervelopment Posted November 18, 2009 Author Share Posted November 18, 2009 That worked a treat .. Thank you so much for your help. Your a lifesaver Quote Link to comment https://forums.phpfreaks.com/topic/181954-solved-query-help/#findComment-959774 Share on other sites More sharing options...
Alex Posted November 18, 2009 Share Posted November 18, 2009 No problem. By the way, there's a "Topic solved" feature, whenever a topic you posted is solved just make sure to mark it as solved, there's a button in the bottom left to do so. Quote Link to comment https://forums.phpfreaks.com/topic/181954-solved-query-help/#findComment-959778 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.