Jump to content

Why Query Fails To Pull All Data From Data Base ?


phpsane

Recommended Posts

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"; 
	

Link to comment
Share on other sites

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 by phpsane
Link to comment
Share on other sites

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 by phpsane
Link to comment
Share on other sites

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.

  • Thanks 1
Link to comment
Share on other sites

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"")
	

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.