/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] my query returns null

Hello everyone.

Can someone tell me what’s wrong in my script, my query returns nothing, I’m having a hard time debugging.

I’ll post the snippet of the code where I think the code is wrong.

[code=php]
if($search == “Master”)
{
if($cat1 == 1){
if($cat2 == 0){
$age .= ‘AND age <= 20’;
}
if($cat2 == 1){
$age .= ‘AND age >= 21 AND age <= 30’;
}
if($cat2 == 2){
$age .= ‘AND age >= 31 AND age <= 40’;
}
if($cat2 == 3){
$age .= ‘AND age >= 41 AND age <= 50’;
}
if($cat2 == 4){
$age .= ‘AND age >= 51 AND age <= 60’;
}
}
if($cat1 == “”){
$age .= ‘AND age <= 60’;
}
if($cat3 == 1){
if($cat4 == 0){
$exp .= ‘AND years_exp == 1’;
}
if($cat4 == 1){
$exp .= ‘AND years_exp == 2’;
}
if($cat4 == 2){
$exp .= ‘AND years_exp == 3’;
}
if($cat4 == 3){
$exp .= ‘AND years_exp == 4’;
}
if($cat4 == 4){
$exp .= ‘AND years_exp >= 5’;
}
}
else
{
$exp .= ‘AND years_exp >= 1’;
}
}
$result = mysql_query(“SELECT * FROM users WHERE position_applied
LIKE ‘%$search%'” . $age . $exp);
[/code]

$cat1, $cat2, $cat3, $cat4 values came from a form, I try to echo each variables and as I expected it has a value. Say my value are this:

$search = Master; $cat1 = 1; $cat2 = 2; $cat3 = 1; $cat4 = 2;

Which means my $age will be equal to ‘AND age >= 31 AND age <= 40’ and $exp will be ‘AND years_exp == 3’, and that will be the one inserted in the query. However it seems that the query can’t contain it. ?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiJan 11.2010 — The best thing it to echo your sql once it's built. If you do you will see that you are not leaving spaces between the last &#37; in the LIKE condition and the start of the age condition. Likewise between age and exp.

Also just a small tip, you can use BETWEEN for neater range conditions:

SELECT <span><code>foo</code></span> FROM <span><code>bar</code></span> WHERE <span><code>age</code></span> BETWEEN 20 AND 35

You should also look at the manual page for mysql_error().

BTW just to be pedantic, your call to mysql_query returns false, not null. I don't say that to be annoying, but when reporting issues it is good to be as specific as possible.
Copy linkTweet thisAlerts:
@kingdmauthorJan 11.2010 — Thanks Mindzai.

It's strange, I did made it work by placing a space on the start of each $exp. Making it look like ' AND years_exp >=1'.

Thanks for tipping me about the BETWEEN.
Copy linkTweet thisAlerts:
@kingdmauthorJan 11.2010 — BTW just to be pedantic, your call to mysql_query returns false, not null. I don't say that to be annoying, but when reporting issues it is good to be as specific as possible.[/QUOTE]

Sorry ? I thought its null, sorry wont happen again.
Copy linkTweet thisAlerts:
@MindzaiJan 11.2010 — Don't worry I wasn't telling you off! Just letting you know that false and null are not the same thing.
Copy linkTweet thisAlerts:
@kingdmauthorJan 11.2010 — Don't worry I wasn't telling you off! Just letting you know that false and null are not the same thing.[/QUOTE]

Now I know that false and null are not the same, thanks for teaching me. ?
×

Success!

Help @kingdm 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 6.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

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