/    Sign up×
Community /Pin to ProfileBookmark

Why Query Fails To Pull All Data From Data Base ?

Hi,

I want to pull data from db, where sometimes all rows and sometimes rows matching given “username”.

Here is my code:

[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”; 
    } 

[/code]

When I specify a “username” in the query via the url:

browsing_histories_v1.php?followee_username=scottee_boy&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=followee_all&page_number=1

This query shouldv’e worked:

[code]

$query = “SELECT * FROM browsing_histories”; 

[/code]

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@site-developerauthorJan 25.2019 — Neither the IF from here shows all rows:

<i>
</i>
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!
Copy linkTweet thisAlerts:
@site-developerauthorJan 25.2019 — Correction:

This url should work:

browsing_histories_v1.php?followee_username=followee_all&page_number=1

It should trigger:

<i>
</i>$query = "SELECT * FROM browsing_histories";


It should show all rows from the tbl. Correct ?

What's wrong ? Puzzled!
Copy linkTweet thisAlerts:
@ginerjmJan 25.2019 — How about this:


if($followee_username != "followee_all" AND $followee_username != "Followee_All")
Copy linkTweet thisAlerts:
@NogDogJan 25.2019 — You say you're entering "all" in the URL but you're checking for "followee_all" in the code. _(EDIT: Oh, I see in a later post you corrected that.)_

On a side note, you're entering user input directly into your SQL, which is a recipe for disaster if someone attempts a SQL injection attack on your site. Either use the applicable escaping function for whichever database extension you're using, or use a prepared statement with a bound input parameter if using an extension that supports that (such as PDO or MySQLi).
Copy linkTweet thisAlerts:
@site-developerauthorJan 25.2019 — @NogDog#1600363

Thanks.

But, why my queries fail ?
Copy linkTweet thisAlerts:
@site-developerauthorJan 25.2019 — @ginerjm#1600362

You did it with the AND and I did it with OR:

1 Example
<i>
</i>if($followee_username == "followee_all" OR $followee_username == "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";
}

2 Example
<i>
</i> if($followee_username != "followee_all" OR $followee_username != "Followee_All")
{
$query = "SELECT * FROM following_histories WHERE followee_username = "$followee_username"";
$query_type = "followee_username";
$followed_word = "$followee_username";
$followee_username = "$followee_username";
$follower_username = "$user";
echo "$followee_username";
}
else
{
$query = "SELECT * FROM following_histories";
$query_type = "followee_all";
$followed_word = "followee_all";
$follower_username = "$user";
echo "all";
}


Both wrong ? It should be AND ? Or, was it typo on your part ?
Copy linkTweet thisAlerts:
@NogDogJan 25.2019 — Okay, let's start by telling us which database extension you are using (which might help me understand why you have double quotes instead of single quotes around the string literal within your query).

As far as your AND/OR issue, you can get rid of it entirely by converting the value to all lower-case in the comparison:
<i>
</i>if(strtolower($followee_username) == 'followee_all')
{
// query for all records
}

You could make the code DRYer with something like:
<i>
</i>$query = "SELECT * FROM following_histories";
if(strtolower($followee_username) != 'followee_all')
{
$query .= "nWHERE followee_username = "$followee_username"";
}

In any case, without some logging or debug output to see for sure the exact queries being used in each case, we may be missing something more basic in the query itself. ?
Copy linkTweet thisAlerts:
@ginerjmJan 25.2019 — My bad - such a hurry to get the statement corrected that I swapped the Or out for an And. Wrong! Basically what works in English doesn't work in the SQL language. YOu have to be explicit in your comparisons and you can't assume that the interpreter will understand your intent.
Copy linkTweet thisAlerts:
@HidDR_5Jan 29.2019 — @NogDog#1600368 so, why queries fail ?
Copy linkTweet thisAlerts:
@rootJan 29.2019 — FUNDAMENTAL OF ANY PROGRAMMING OF A CONDITIONAL STATEMENT...

This is wrong if($followee_username == "followee_all" OR "Followee_All")

you can not use an OR like that... if($followee_username == "followee_all" OR $followee_username =="Followee_All")
Copy linkTweet thisAlerts:
@site-developerauthorFeb 02.2019 — @NogDog#1600368

Sorry for the late reply. Ok, switching to sngl quotes. Thanks!
×

Success!

Help @site-developer spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...