phpsane Posted January 25, 2019 Share Posted January 25, 2019 Hi, I want to pull data from db, where sometimes all rows and sometimes rows matching given "username". Here is my code: //Grab Username of who's Browsing History needs to be searched. if (isset($_GET['followee_username']) && !empty($_GET['followee_username'])) { $followee_username = $_GET['followee_username']; if($followee_username != "followee_all" OR "Followee_All") { $query = "SELECT * FROM browsing_histories WHERE username = \"$followee_username\""; $query_type = "followee_username"; $followed_word = "$followee_username"; $follower_username = "$user"; echo "$followee_username"; } else { $query = "SELECT * FROM browsing_histories"; $query_type = "followee_all"; $followed_word = "followee_all"; $follower_username = "$user"; echo "all"; } } When I specify a "username" in the query via the url: browsing_histories_v1.php?followee_username=requinix&page_number=1 I see result as I should. So far so good. Now, when I specify "all" as username then I see no results. Why ? All records from the tbl should be pulled! browsing_histories_v1.php?followee_username=all&page_number=1 This query shouldv'e worked: $query = "SELECT * FROM browsing_histories"; Quote Link to comment Share on other sites More sharing options...
phpsane Posted January 25, 2019 Author Share Posted January 25, 2019 (edited) Neither the IF from here shows all rows: if($followee_username == "followee_all" OR "Followee_All") { $query = "SELECT * FROM following_histories"; echo "all"; $query_type = "followee_all"; $followed_word = "followee_all"; $follower_username = "$user"; } else { $query = "SELECT * FROM following_histories WHERE username = \"$followee_username\""; echo "$followee_username"; $query_type = "$followee_username"; $followed_word = "$followee_username"; $follower_username = "$user"; } Why it fails ? Puzzled! Edited January 25, 2019 by phpsane Quote Link to comment Share on other sites More sharing options...
phpsane Posted January 25, 2019 Author Share Posted January 25, 2019 (edited) Correction: This url should work: browsing_histories_v1.php?followee_username=followee_all&page_number=1 It should trigger: $query = "SELECT * FROM browsing_histories"; It should show all rows from the tbl. Correct ? What's wrong ? Puzzled! Edited January 25, 2019 by phpsane Quote Link to comment Share on other sites More sharing options...
requinix Posted January 25, 2019 Share Posted January 25, 2019 if($followee_username != "followee_all" OR "Followee_All") Do you know how the "or" operator works? The only correct response to that question is no. Learn what "or" means. 1 Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted January 25, 2019 Share Posted January 25, 2019 As requinix suggested, you need to be using the logical operator for "and". Note that you'll also need to correct the second part of your if statement. The following will always equate to "true". if("Followee_All") { So the following is going to give you unexpected results: if($followee_username != "followee_all" AND "Followee_All") { You need to tell PHP that you are running a second test against the $followee_username variable. if($followee_username != "followee_all" AND $followee_username != "Followee_All") { For what it's worth, I would just leave the username blank whenever someone wants to see all users. Then your code would look something like this: //IF USERNAME ISN'T BLANK, GET SELECTED USER'S INFORMATION if($followee_username != "") { //... //ELSE...GET INFORMATION FOR ALL USERS } else { //... } That way you don't need to worry about the case (followee_all, Followee_All, FoLLowee_ALL). You also don't need to worry about someone inadvertently picking this "special" username. Of course, I'm guessing that's probably not going to happen. 1 Quote Link to comment Share on other sites More sharing options...
phpsane Posted February 2, 2019 Author Share Posted February 2, 2019 On 1/25/2019 at 11:48 PM, requinix said: if($followee_username != "followee_all" OR "Followee_All") Do you know how the "or" operator works? The only correct response to that question is no. Learn what "or" means. I spotted my error the other day. Should've been: if($followee_username != "followee_all" OR $followee_username != "followee_All"") Quote Link to comment Share on other sites More sharing options...
requinix Posted February 2, 2019 Share Posted February 2, 2019 3 hours ago, phpsane said: I spotted my error the other day. Good job, but you haven't understood the whole problem. If the value of $followee_username is "followee_All" then what will the if statement do? Quote Link to comment 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.