SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Digital Warrior Renegade's Avatar
    Join Date
    Nov 2000
    Location
    Portland, OR
    Posts
    480
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Building dynamic SQL statements...

    I have been struggling for quite a while on this subject, I am hoping someone can shed some light on it.

    I have a PHP4 search page, backended with a MySQL DB. There are 6 fields that the user can search the database for. The search page will allow any combo of the 6 fields to be inputted into the query.

    Now, I could write the SQL statement no problem. Sure, I just open up a console and type it in. My problem, is trying to get PHP to build the SQL statement based on what the user typed/selected in the form.

    My biggest problem really, are commas and "AND"'s I usually have one of the two where I don't need it, or I don't have one where I DO need it, and of course it screws up the query

    How do I go about doing this? Can anyone bless me with their profound knowledge of the subject? Thanks in advance...

  2. #2
    :) delemtri's Avatar
    Join Date
    Jun 2001
    Posts
    579
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I used eregi() the one time I built a search engine.

  3. #3
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well one way of building the where query is:

    $where = "WHERE id > 0";

    if ($email != "") {
    $where .= " AND email = '$email'";
    }
    if ($website != "") {
    $where .= " AND website = '$email'";
    }

    etc...

    the WHERE id > 0 is a cheat really, there is another way to do it but requires more programming to check there was at least one input and then just have a "WHERE " etc..


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
  •