/    Sign up×
Community /Pin to ProfileBookmark

How do i get the "SELECT" string correct? And wrong

haha my subject….too bad i cant change it

Im trying to make it possible to search people after age so this is what ive done. problem is, the string looks like this and obv that doesnt work.

[code=html]
SELECT * FROM users WHERE country=’asd’ AND state=’zxc”’ > ”” < 1970-01-01 [/code]

i have been trying to get it to work but i cant. This is the code.

[code=php]
$query = “SELECT * FROM users WHERE country=’$country’ AND state=’$state'”;

$age= $row[‘age’];
if(!is_null($mindate))
$query .= ” AND age='”;
$query .= “‘$age’ > ‘$mindate'”;
if(!is_null($maxdate))
if(!is_null($mindate))
$query .= ” AND “;
$query .= “‘$age’ < $maxdate”;[/code]

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 29.2015 — I suspect the problem is that you are not using braces to delineate the code associated with each if(), and in PHP it will only associate the next line with it if you do not use braces. So, your code as written is equivalent to:
[code=php]
$query = "SELECT * FROM users WHERE country='$country' AND state='$state'";

$age= $row['age'];
if(!is_null($mindate))
{
$query .= " AND age='";
}
$query .= "'$age' > '$mindate'";
if(!is_null($maxdate))
{
if(!is_null($mindate))
{
$query .= " AND ";
}
}
$query .= "'$age' < $maxdate";

[/code]

Which I suspect is not what you want. ?

This is why many (most?) PHP devs used braces around if/else code blocks even if it's just for one line, in case they ever want to add lines to them.
Copy linkTweet thisAlerts:
@ginerjmOct 29.2015 — Also - what does age look like? Is it something like 12 rather than a date value? If so you can't compare that age value to a true date value. Such as "is 12 > 12/31/52". If age however is really a date then you are simply guilty of not using 'meaningful variable names'.
Copy linkTweet thisAlerts:
@jag1authorOct 29.2015 — Thank you guys. I think i got the string correct now its just the y-m-d who show 1970-01-01 all the time.

Ginerjm im trying to make both in y-m-d like this hm:

$mindate = is_numeric($a2)? date("Y-m-d", strtotime('-$a2 year')) : null;

$maxdate = is_numeric($a1)? date("Y-m-d", strtotime('-$a1 year')) : null;

Input age in $a2 and $a1(25, 75,15)
Copy linkTweet thisAlerts:
@ginerjmOct 29.2015 — ???
Copy linkTweet thisAlerts:
@Aaronique_BainJan 27.2016 — What is the best hit counter for a site?
Copy linkTweet thisAlerts:
@ginerjmJan 27.2016 — not nice to hijack a thread for your own uses.

Stand up and create your own post for yourself
Copy linkTweet thisAlerts:
@Aaronique_BainJan 28.2016 — how to create my own post?
Copy linkTweet thisAlerts:
@TrainJan 28.2016 — Scroll to the top

click the PHP

That will open up a new page and toward the left you will see a POST NEW THREAD button.

Or use this

http://www.webdeveloper.com/forum/forumdisplay.php?16-PHP
×

Success!

Help @jag1 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.11,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

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