#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2000
    Posts
    5
    Rep Power
    0
    Trying to get away from access and asp I
    recently read Website Database Basics With PHP and MySQL by Thomas Kehoe Good tutorial - I learned a lot ! my problem? is how do you search on an array ? I am currently use explode to split a variable up into parts with a space delimiter but now I need to do a query in mysql using this array - how ? eg say the variable was "yellow dog" I need to search the database for all occurances of yellow,dog and yellow dog. ie sample result would be :- yellow dog yellow cat black dog purple dog yellow cow possible ?
    Oh yes I have RTFM i been bashing my head against a wall for two days now !



    [This message has been edited by luna_securit (edited January 19, 2000).]
  2. #2
  3. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    18
    You would have to dynamically create your query since you don't know how many words would be used in the search. Let's say you want to return the entire row, $ary contains the split words, and 'data' is the field you are searching on:

    $query="select * from table where ";
    while ($datum=each($ary))
    {
    $query.="data LIKE '%$datum[value]%' OR ";
    }
    // strip off the last 'OR'
    $query=substr($query,0,-4);

    Then run your query. NOTE: this code is untested.

    [This message has been edited by rod k (edited January 19, 2000).]

    [This message has been edited by rod k (edited January 19, 2000).]
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2000
    Posts
    5
    Rep Power
    0
    Thanks for you reply - but where would it fit in this ??

    $pieces = explode(" ", $Description);

    print $Description;
    print $pieces[0];

    if ($ManPart == "")
    {$ManPart = '%';}

    if ($Franchise == "")
    {$Franchise = '%';}

    $result = mysql_query ("SELECT * FROM oldparts
    WHERE Partno LIKE '%$Partno%'

    AND Description LIKE '%$Description%; // this is the field I need the array search on... would it be something like this ???? ->
    while ($datum=each($pieces))
    {
    $query.="$Description LIKE '%$datum[value]%' OR ";
    }
    // strip off the last 'OR'
    $query=substr($query,0,-4); <--

    AND ManPart LIKE '%$Manpart%'
    AND Franchise LIKE '%$Franchise%'
    ") or die("no records found") ;

    [This message has been edited by luna_securit (edited January 19, 2000).]

    [This message has been edited by luna_securit (edited January 19, 2000).]
  6. #4
  7. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    18
    Yes, except:

    $query.="$Description LIKE '%$datum[value]%' OR ";


    should be

    $query.="Description LIKE '%$datum[value]%' OR ";

    no $ in front of the field name

    and you need an OR after Description LIKE '%$Description%'


    [This message has been edited by rod k (edited January 19, 2000).]
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2000
    Posts
    5
    Rep Power
    0
    Nothing seems to work

    I tried the above and I get a parse error with the line :-
    $query.="'%$datum[value]%' AND Description LIKE ";

    so I created a variable using the code
    so it looked like this :-

    $pieces = explode(" ", $Description);

    $query="LIKE ";
    while ($datum=each($pieces))
    {
    $query.="'%$datum[value]%' AND Description LIKE ";
    }
    // strip off the last 'AND'
    $query=substr($query,0,-21);

    this returned when printed :-

    LIKE '%water%' AND Description LIKE '%pump%'

    great now all I had to do was :-

    $result = mysql_query ("SELECT * FROM oldparts
    WHERE Partno LIKE '%$Partno%'
    AND Description '$query' // should equal LIKE '%water%' AND Description LIKE '%pump%
    AND ManPart LIKE '%$Manpart%'
    AND Franchise LIKE '%$Franchise%'

    this in theory shoud've worked but I get a :-

    Warning: 0 is not a MySQL result index in c:localhostsearchform.php3 on line 48

    I've tried allsorts help ?


Similar Threads

  1. Newbie Multidimentional Arrays Question
    By usurpian in forum C Programming
    Replies: 10
    Last Post: January 13th, 2004, 10:40 AM
  2. help debug this code (newbie alert!)
    By dave2k in forum Java Help
    Replies: 4
    Last Post: December 12th, 2003, 04:58 PM
  3. wats wrong with my method? newbie alert!
    By dave2k in forum Java Help
    Replies: 4
    Last Post: November 10th, 2003, 10:44 AM
  4. [Advanced] I/Frame accessibility
    By csaba in forum HTML Programming
    Replies: 1
    Last Post: June 8th, 2003, 09:08 AM
  5. arrays and string common libaries (newbie question)
    By gargoyle in forum C Programming
    Replies: 5
    Last Post: April 27th, 2003, 08:24 PM

IMN logo majestic logo threadwatch logo seochat tools logo