pioneerx01 Posted January 19, 2011 Share Posted January 19, 2011 Why is this not working? I can not seem to incorporate and into the statement $query = "SELECT * FROM Project_Registrations WHERE (First_Name = '$_POST[name_1]' AND Teacher = '$_POST[teacher]' AND School = '$_POST[school]') OR (Second_Name = '$_POST[name_1]' AND Teacher = '$_POST[teacher]' AND School = '$_POST[school]') OR (First_Name = '$_POST[name_2]' AND Teacher = '$_POST[teacher]' AND School = '$_POST[school]') OR (Second_Name = '$_POST[name_2]' AND Teacher = '$_POST[teacher]' AND School = '$_POST[school]') "; Thanks Quote Link to comment https://forums.phpfreaks.com/topic/224915-more-complicated-select-from-where-and-or-statement/ Share on other sites More sharing options...
Pikachu2000 Posted January 19, 2011 Share Posted January 19, 2011 Have you echoed it to make sure it contains the values you'd expect it to contain? If it did, did you you paste it into phpMyAdmin and see what results it returned? Quote Link to comment https://forums.phpfreaks.com/topic/224915-more-complicated-select-from-where-and-or-statement/#findComment-1161730 Share on other sites More sharing options...
Muddy_Funster Posted January 19, 2011 Share Posted January 19, 2011 "It's not working" ? any chance of giving a little detail here? ever went to the doctor and said "I don't feel right" and have them fix the problem from that with no other input from you? @ Pikachu - you know they havn't echoed it out Quote Link to comment https://forums.phpfreaks.com/topic/224915-more-complicated-select-from-where-and-or-statement/#findComment-1161797 Share on other sites More sharing options...
DarkKnight2011 Posted January 19, 2011 Share Posted January 19, 2011 i dont think your building the string correctly, try this and let me know if it works.... $query = "SELECT * FROM Project_Registrations WHERE (First_Name = '". $_POST[name_1] . "' AND Teacher = '". $_POST[teacher] ."' AND School = '". $_POST[school] ."') OR (Second_Name = '". $_POST[name_1] ."' AND Teacher = '". $_POST[teacher] ."' AND School = '". $_POST[school] ."') OR (First_Name = '". $_POST[name_2] ."' AND Teacher = '". $_POST[teacher] ."' AND School = '". $_POST[school] ."') OR (Second_Name = '". $_POST[name_2] ."' AND Teacher = '". $_POST[teacher] ."' AND School = '". $_POST[school] ."') Try that and see what happens, if not echo out the result of yours and mine and see whats shown, you could then paste the displayed SQL statements directly into mysql_workbench or phpmyadmin (whichever you use) and that will show you where the problem is BTW - This is my first time ever responding to a PHP post, so if im well off - be nice Quote Link to comment https://forums.phpfreaks.com/topic/224915-more-complicated-select-from-where-and-or-statement/#findComment-1161851 Share on other sites More sharing options...
Pikachu2000 Posted January 19, 2011 Share Posted January 19, 2011 i dont think your building the string correctly, try this and let me know if it works.... $query = "SELECT * FROM Project_Registrations WHERE (First_Name = '". $_POST[name_1] . "' AND Teacher = '". $_POST[teacher] ."' AND School = '". $_POST[school] ."') OR (Second_Name = '". $_POST[name_1] ."' AND Teacher = '". $_POST[teacher] ."' AND School = '". $_POST[school] ."') OR (First_Name = '". $_POST[name_2] ."' AND Teacher = '". $_POST[teacher] ."' AND School = '". $_POST[school] ."') OR (Second_Name = '". $_POST[name_2] ."' AND Teacher = '". $_POST[teacher] ."' AND School = '". $_POST[school] ."') Try that and see what happens, if not echo out the result of yours and mine and see whats shown, you could then paste the displayed SQL statements directly into mysql_workbench or phpmyadmin (whichever you use) and that will show you where the problem is BTW - This is my first time ever responding to a PHP post, so if im well off - be nice That isn't any different from the code in the OP, functionally. Quote Link to comment https://forums.phpfreaks.com/topic/224915-more-complicated-select-from-where-and-or-statement/#findComment-1161877 Share on other sites More sharing options...
DarkKnight2011 Posted January 19, 2011 Share Posted January 19, 2011 yeah i wasnt trying to change functionality, i just didnt think that the string looked like it had been created properly and that the values wouldnt be quoted correctly to run on mysql Quote Link to comment https://forums.phpfreaks.com/topic/224915-more-complicated-select-from-where-and-or-statement/#findComment-1161882 Share on other sites More sharing options...
Pikachu2000 Posted January 19, 2011 Share Posted January 19, 2011 By no different functionally, I mean the output from the two will be identical if both were echoed. There's no point in concatenating all the values; it makes no difference at all in the end result. Quote Link to comment https://forums.phpfreaks.com/topic/224915-more-complicated-select-from-where-and-or-statement/#findComment-1161884 Share on other sites More sharing options...
PFMaBiSmAd Posted January 19, 2011 Share Posted January 19, 2011 Concatenation with that many variables also results in a huge number of typos and the resulting php syntax errors. @pioneerx01 your query is LOGICALLY correct SQL, though it can be greatly simplified. If it's not working, then either your variables don't have the values you expect (as already suggested) or your query is producing an error of some kind (I'm not going to bother to list all of the possibilities because your error checking and error reporting logic in your code should be telling you if a query error occurred) or you aren't executing the query (we see that a lot) or you aren't fetching the results correctly from the query (we see that a lot too)... Quote Link to comment https://forums.phpfreaks.com/topic/224915-more-complicated-select-from-where-and-or-statement/#findComment-1161887 Share on other sites More sharing options...
PFMaBiSmAd Posted January 19, 2011 Share Posted January 19, 2011 Speaking of greatly simplified sql, the following should (untested) work (assuming your table/columns names are correct, you are escaping data, your data has expected values...) - $names = "'{$_POST['name_1']}','{$_POST['name_2']}'"; $query = "SELECT * FROM Project_Registrations WHERE Teacher = '{$_POST['teacher']}' AND School = '{$_POST['school']}' AND (First_Name IN($names) OR Second_Name IN($names))"; Quote Link to comment https://forums.phpfreaks.com/topic/224915-more-complicated-select-from-where-and-or-statement/#findComment-1161895 Share on other sites More sharing options...
DarkKnight2011 Posted January 19, 2011 Share Posted January 19, 2011 Ah my bad, i just tested it and the Concatenation works either way i didnt think that " '$_POST[name]' " would work, there are always those small things that people miss or forget So Thanks Anyway, back to solving it, firstly like other people have already stated, make sure that your getting the correct values, your running the query, and fetching results (make sure you have posted values), if its failing at any of those points just let us know where and what errors you get and im sure someone will be able to help. If you do need to post back for assistance i would suggest that you echo your query and post all your code and any error messages your getting or explain your unexpected results Quote Link to comment https://forums.phpfreaks.com/topic/224915-more-complicated-select-from-where-and-or-statement/#findComment-1161897 Share on other sites More sharing options...
pioneerx01 Posted January 23, 2011 Author Share Posted January 23, 2011 Sorry, I meant to ask if this code was formulated correctly. And yes all the values are there and are what they should be. PFMaBiSmAd Thank you, Your code seems to work as I wanted to. Thank you to all. Quote Link to comment https://forums.phpfreaks.com/topic/224915-more-complicated-select-from-where-and-or-statement/#findComment-1163807 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.