Mysql query problem

Discussion in 'PHP' started by hasbehas, Mar 25, 2008.

  1. #1
    I had this php+mysql db done for me long time ago.
    Before, a property was either "house" or "apartment" or "new development". Selected from a dropdown..

    mysql field name was "type". so properties.php?type=house or properties.php?type=new-development or properties.php?type=apartment was working fine. Even properties.php?type=house&type2=new-development would bring both the "houses" and "new-developments"

    Now I am trying to make some properties "house" and "new development" at same time.

    So I have altered my property edit screen to be ;
    *house
    *apartment
    *new-development
    *etc etc..

    Altered my db to be "type1 - house" , "type2 - apartment" , "type3 - new-development" etc etc..

    But I can not get the mysql query line right to list the properties I want.. Can anybody give me a hand ?

    my old code;
    
    
    if ($bedroom)
    {
    $bedroomsearch = " and bedrooms > '$bedroom' ";
    }
    
    if ($seaview)
    {
    $seaviewsearch = " and seaview like '%on%' ";
    }
    
    if ($security)
    {
    $securitysearch = " and security like '%on%' ";
    }
    
    if ($furniture)
    {
    $furnituresearch = " and furniture like '%on%' ";
    }
    
    if ($state)
    {
    $statesearch = " and state like '%$state%' ";
    }
    
    if ($type1)
    {
    $typesearch = " and type1 like '%$type1%' ";
    }
    
    if ($type2)
    {
    $typesearch = " and (type1 like '%$type1%' and type2 like '%$type2%') ";
    }
    
    if ($type3)
    {
    $typesearch = "  and (type1 like '%$type1%' and type2 like '%$type2%' and type3 like '%$type3%') ";
    }
    if ($type4)
    {
    $typesearch = "  and (type1 like '%$type1%' and type2 like '%$type2%' and type3 like '%$type3%' and type4 like '%$type4%') ";
    }
    
    
    //----------------------------------------------------------------------
    if (!isset($oteleme) || $oteleme < 0) $oteleme=0;
    if (!isset($dizeindex) || $dizeindex < 0) $dizeindex=0;
    //----------------------------------------------------------------------
    
    if (!$pricemin)
            {
            $check = mysql_query("select * from $table where location like '%$location%' and state like '%$state%' and status='4' $seaviewsearch $securitysearch $furnituresearch $statesearch $bedroomsearch $typesearch $typesearch2 $typesearch3 $typesearch4 order by ID desc LIMIT $oteleme,$KayitLimiti");
            }
    
    elseif ($pricemin)
            {
            $check = mysql_query("select * from $table where location like '%$location%' and state like '%$state%' and price >= '$pricemin' and price <= '$pricemax' and status='4' $seaviewsearch $securitysearch $furnituresearch $statesearch $bedroomsearch $typesearch $typesearch2 $typesearch3 $typesearch4 order by ID desc LIMIT $oteleme,$KayitLimiti");
            }
    
            $numf = mysql_num_rows($check);
    
    $toplam_satiri_al = $check;
    $toplam_satir     = mysql_num_rows($toplam_satiri_al);
    
    if ($numf == 0)
            {
            echo "<center><br><br><br><b><font face=verdana size=5 color=red>Nothing found..!</b></font>";
            exit;
            }
    
    
    PHP:
    What should my new code be ???
     
    hasbehas, Mar 25, 2008 IP
  2. EvinDesign

    EvinDesign Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    there are 2 solutions :

    1. you simply put ' ' around your vars

    
    $check = mysql_query("select * from $table where location like '%$location%' and state like '%$state%' and price >= '$pricemin' and price <= '$pricemax' and status='4' '$seaviewsearch' '$securitysearch' etc.
    
    Code (markup):
    2. You do a better solution:

    lets say that $check is your query var, declare it first then you simply add to it as things turns out true like this

    
    $check = "SELECT blabla..";
    
    if ($seaview == "1")
    {
    $check .= " and seaview like '%on%' ";
    }
    
    //Finally to wrap it up
    $check .= "order by ID desc LIMIT $oteleme,$KayitLimiti";
    
    Code (markup):
    I prefer the last method as it is more versitile..
     
    EvinDesign, Mar 25, 2008 IP
  3. hasbehas

    hasbehas Well-Known Member

    Messages:
    726
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    190
    #3
    Thanks for the tip;
    
    //----------------------------------------------------------------------
    if (!isset($oteleme) || $oteleme < 0) $oteleme=0;
    if (!isset($dizeindex) || $dizeindex < 0) $dizeindex=0;
    //----------------------------------------------------------------------
    
    $check = mysql_query("select * from $table where location like '%$location%' and state like '%$state%' and status='4'");
    
    if ($type1 != "")
    {
    $check .= " and type1 like '%$type1%' ";
    }
    if ($type2 != "")
    {
    $check .= " and type2 like '%$type2%' ";
    }
    if ($type3 != "")
    {
    $check .= " and type3 like '%$type3%' ";
    }
    if ($pricemin != "")
    {
    $check .= " and price >= '$pricemin' and price <= '$pricemax' ";
    }
    else 
    { 
    $check .= "order by ID desc LIMIT $oteleme,$KayitLimiti";
    
    $numf = mysql_num_rows($check);
    }
    $toplam_satiri_al = $check;
    $toplam_satir     = mysql_num_rows($toplam_satiri_al);
    
    if ($numf == 0)
            {
            echo "<center><br><br><br><b><font face=verdana size=5 color=red>Nothing found..!</b></font>";
            exit;
            }
    
    PHP:
    but this code returns ;

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/trealest/public_html/test/properties.php on line 80


    What did I do wrong ?
    line 80 ; $numf = mysql_num_rows($check);
     
    hasbehas, Mar 26, 2008 IP