Showing posts with label php Why doesn't the SELECT query select what it should select?. Show all posts
Showing posts with label php Why doesn't the SELECT query select what it should select?. Show all posts

Why doesn't the SELECT query select what it should select?

Wednesday, November 4, 2015

I have never experienced this before so I wonder what to look after in the code to solve it.



This is the code - which should work?



    $time = strtotime("-$a2 year", time());
$date = date("Y", $time);

$query = "SELECT * FROM users WHERE country='$country' AND state='$state'";

if(!empty($a2) && empty($a1)){
$query .= " AND year<=";
$query .= " '$date'";
$query .= " AND day<=";
$query .= " '$todayd'";
$query .= " AND month<=";
$query .= " '$todaym'";
}
if(..){}
if(..){}
$result = $query;

$query = mysqli_query($con,"$result");

while($row = mysqli_fetch_array($query))
{
.......


If I echo the string $query it shows:



SELECT * FROM users WHERE country='Spain' AND state='Madrid'


But the selected/shown users have all different kinds of countires and states

Why doesn't the SELECT query select what it should select?

I have never experienced this before so I wonder what to look after in the code to solve it.



This is the code - which should work?



$query = "SELECT * FROM users WHERE country='$country' AND state='$state'";
$result = $query;

$query = mysqli_query($con,"$result");

while($row = mysqli_fetch_array($query))
{
.......


If I echo the string $query it shows:



SELECT * FROM users WHERE country='Spain' AND state='Madrid'


But the selected/shown users have all different kinds of countires and states