Jump to content

sql query by php trouble


LeadingWebDev

Recommended Posts

hi all.

i help, but now i need help  :shy:

 

I have a search form for real estate, every thing work well, except floors that works, but not completely.

i got to construct query if var so $query .="..."; constructed and works.

 

The trouble is:

floor -> <option value="0">Ground</option>

 

ONLY when $floor=0 i dont get any result, but the query success. else if select 1+ im getting results as needed.=)

Echoed $floor, got floor: 0 - works.

tried same query in SQLyog -> works well with 0.

take a look:

// QUERY CONSTRUCTOR BEGIN

$sqlRequest = "SELECT * FROM `sell_estate` WHERE `estate_type` = '$estate_type' AND `region` = '$region'";

//Constructing proper query part for floors
if($from_floor && $to_floor)
{
	if($from_floor == 0 || $to_floor == 0)
	{
		if($from_floor == 0 && $to_floor == 0)
		{
			$sqlRequest .=" AND floor BETWEEN '0' AND '0'";
		}
		elseif($from_floor == 0 && $to_floor > 0)
		{
			$sqlRequest .=" AND floor BETWEEN '0' AND '$to_floor'";
		}
		elseif($from_floor > 0 && $to_floor == 0)
		{
			echo "max floor cant be lower than min floor";
		}
	}
	else
	{
		$sqlRequest .=" AND floor BETWEEN '$from_floor' AND '$to_floor'";
	}
}else{
	if($from_floor && !$to_floor)
	{
		$sqlRequest .=" AND floor >= '$from_floor'";
	}
	elseif($to_floor && !$from_floor)
	{
		$sqlRequest .=" AND floor <= '$to_floor'";
	}
}
//End Constructing proper query part for floors
//Query ends with sorting by ad Post Date DESCEND.

$sqlRequest .=" ORDER BY `post_date` DESC";

// QUERY CONSTRUCTOR END

 

 

Link to comment
https://forums.phpfreaks.com/topic/192502-sql-query-by-php-trouble/
Share on other sites

anyone?

Bump?

 

it was looking before like that

				//Constructing proper query for floors
if($from_floor && $to_floor)
{
        	if($to_floor > $from_floor)
                {
                echo "Max floor cant be lower than min floor.";
                }else{
	$sqlRequest .=" AND floor BETWEEN '$from_floor' AND '$to_floor'";
                {
}else{
	if($from_floor && !$to_floor)
	{
		$sqlRequest .=" AND floor >= '$from_floor'";
	}
	elseif($to_floor && !$from_floor)
	{
		$sqlRequest .=" AND floor <= '$to_floor'";
	}
}
//End Constructing proper query for floors

just do this

 

//incase u are not sure that u have these var's as numbers
$from_floor = intval($from_floor );
$to_floor = intval($to_floor );

$sqlRequest = "SELECT * FROM `sell_estate` WHERE `estate_type` = '$estate_type' AND `region` = '$region'";
$sqlRequest .=" AND floor BETWEEN $from_floor AND $to_floor";

 

btw u never go inside this if "if($from_floor == 0 || $to_floor == 0)"

because u are asking opposite thing above by this "if($from_floor && $to_floor)" <= so when $from_floor=0 and $to_floor=0 it will never be interpretated as TRUE

unsuccess.

 

The problem itself is in '0' that going to the query, any other INTs the script hits no problem, and giving the result

when it hits '0' -> Query Success, no errors appear.

 

looks now like

	//Constructing proper query for floors
$from_floor = intval($from_floor);
$to_floor = intval($to_floor);
if($from_floor && $to_floor)
{

	$sqlRequest .=" AND (floor BETWEEN '$from_floor' AND '$to_floor')";
}else{
	if($from_floor && !$to_floor)
	{
		$sqlRequest .=" AND floor >= '$from_floor'";
	}
	elseif($to_floor && !$from_floor)
	{
		$sqlRequest .=" AND floor <= '$to_floor'";
	}
}
//End Constructing proper query for floors

i also added few lines under...

 

echo "$from_floor and up to $to_floor";

i see "0 and up to 6", but the query...

i also tried to query in SQLyog(if u know), "SELECT * FROM `sell_estate` WHERE `floor` BETWEEN '0' AND '4'" and im getting results... so something wrong, maybe php :/

 

just echoed $sqlRequest:

SELECT * FROM `sell_estate` WHERE `estate_type` = '2' AND `region` = '9' AND floor <= '4' AND rooms BETWEEN '1' AND '2.5' AND estate_size BETWEEN '100' AND '140' AND price <= '1000000' AND currency = 'USD' ORDER BY `post_date` DESC

 

so we see, when i check floor 0, it is coming blank/unavaible, so here is a bug, as i see.

now i will try something. )

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.