SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)

    advanced search script

    Hi,
    I am trying to develop an advanced option search script and found this thread http://www.sitepoint.com/forums/show...ghlight=search but I have come up against a brick wall and now my head hurts.....

    If I put the query as
    PHP Code:
    SELECT FROM table WHERE 1=
    (as suggested in the other thread)
    it makes no difference what I put after it, it still gives all the database records (?)

    What is the easiest way of designing a search that uses option menus from an html form?

    I hope this makes sense to you all.....

    SpikeZ
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    chile
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The posted way is actually the best approach.
    You're probably falling in some other part of the process. Please post your code ;-)

    Aloha,

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    which row is called 1 into your db?
    and what happens if the row 1 is equal to 1?

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Below is the code I am using, I have rewritten it for the purpose of posting but it still gives the same results....

    PHP Code:
    <?php require_once('../Connections/vse.php'); ?>
    <?php
    mysql_select_db
    ($database_vse$vse);
    $advSql "SELECT * FROM vse_villa_details WHERE 1=1";
    $advResult mysql_query($advSql$vse); 
    $row_advResult mysql_fetch_assoc($advResult);

    if(isset(
    $_GET['location'])) 
    $advSql .=" AND state_region = {$location}";


    do {
     echo 
    $row_advResult['villa_id'];
     echo 
    '<br>';
     echo 
    $row_advResult['villa_name'];
     echo 
    '<br>';
     echo 
    $row_advResult['state_region'];
     echo 
    '<br>';
     } while(
    $row_advResult mysql_fetch_assoc($advResult));

     
    ?><br>
    location =  <? echo $location?><br>
    sql =  <? echo $advSql?>
    I have echoed the location search as well as the sql query the results are as follows:

    PHP Code:
    15
    villa_dos
    LA
    14
    villa america
    Florida

    location 
    florida
    sql 
    SELECT FROM vse_villa_details WHERE 1=AND state_region florida 
    Any help would be appreciated

    SpikeZ
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do u read my post?
    which row is called 1
    and why where 1=1
    ie
    PHP Code:
    $id $_SESSION["id"];
    $query $db->query("SELECT *
                         FROM table 
                         WHERE id ="
    $id); 

  6. #6
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    reminder, yes I did read your post so bear with me.

    The reason for 1=1 is that it was suggested in the other thread as a query that will always return true. Currently I have 3 rows in the database 13, 14, 15 but I can change one of those to 1 but it makes no difference....


    Am I missing something blindingly obvious?
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  7. #7
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if noone row is called 1 then this query wont work!!!
    ie.
    ....WHERE row_name = something
    u have
    ....WHERE 1 = 1
    it means that he cant find row called 1 (if it dont exsist) which is equal to something!
    cheers

  8. #8
    Chessplayer kleineme's Avatar
    Join Date
    Apr 2004
    Location
    Germany
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    you are doing the mysql_query before you append any further restrictions to your WHERE clause. The line "$advSql .=" AND state_region = {$location}";" has no effect onto your resultset because it is already fetched at this moment. Move the mysql_query line right after the line where you append the restriction.
    Never ascribe to malice,
    that which can be explained by incompetence.
    Your code should not look unmaintainable, just be that way.

  9. #9
    Chessplayer kleineme's Avatar
    Join Date
    Apr 2004
    Location
    Germany
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Quote Originally Posted by reminder
    if noone row is called 1 then this query wont work!!!
    ie.
    ....WHERE row_name = something
    u have
    ....WHERE 1 = 1
    it means that he cant find row called 1 (if it dont exsist) which is equal to something!
    cheers
    have you tried to use it? WHERE 1=1 is always true so when there are no further restrictions you will get all rows. When there are further restrictions it doesn't hurt because you will get the same result as if it weren't there.

    Unfortunately MySQL really lets you create a column called '1'. I would assume that this is absolutely non-standard, at least it is highly unrecommended. But if you use a WHERE 1=1 with a column '1' present MySQL still evaluates it as a boolean expression. If you want to evaluate the data stored in column '1' you have to put the column name in backticks, i.e. WHERE `1` = 1.
    Never ascribe to malice,
    that which can be explained by incompetence.
    Your code should not look unmaintainable, just be that way.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •