Professional Web Applications Themes

Multiple query variables based on str_word_count - MySQL

Greetings: I am attempting to split an input string using the str_word_count function to return multiple single word query variables. I can create an array based on $search=(str_word_count($search, 1, '0123456789')); (I want words and/or numbers). I can extract named variables from the resulting array via $search=extract($search, EXTR_PREFIX_ALL, search); and then call each variable by $search_0 $search_1 $search_2, etc. Several issues that I am having: Since the size of the array is determined by user input. I can't hardcode $search_x, $search_y, etc. because I don't know what x, y, etc. will be. How can I produce $search_x... et al. and then ...

  1. #1

    Default Multiple query variables based on str_word_count

    Greetings:

    I am attempting to split an input string using the str_word_count
    function to return multiple single word query variables.
    I can create an array based on
    $search=(str_word_count($search, 1, '0123456789')); (I want words and/or
    numbers).
    I can extract named variables from the resulting array via
    $search=extract($search, EXTR_PREFIX_ALL, search);
    and then call each variable by
    $search_0 $search_1 $search_2, etc.
    Several issues that I am having:
    Since the size of the array is determined by user input. I can't
    hardcode $search_x, $search_y, etc. because I don't know what x, y, etc.
    will be. How can I produce $search_x... et al. and then hand it/them to
    a query string? I can echo desired output using for or while loops,
    (i.e. the *value* of $search_x...et al. but I can't figure out how to
    get them into query strings. I want my query strings to contain
    something like

    $q="
    SELECT *
    FROM *
    WHERE table1.row1
    LIKE '%$search_x%'
    OR LIKE '%$search_y%'
    etc..."

    Advice or a sanity check is appreciated.

    --

    Regards,

    Jeff Gardner
    ___________________________

    "Contrary to popular belief, Unix is user friendly. It just happens
    to be very selective about who its friends are." --Kyle Hearn
    Jeff Guest

  2. #2

    Default Re: Multiple query variables based on str_word_count

    Jeff Gardner wrote: 

    Don't bother extracting. Just build your string in a foreach loop, i.e.

    $str = '';
    foreach ($search as $s) {
    if ($str != '')
    $str .= ' OR ';
    $.str .= "LIKE $s";
    }
    $sql = "SELECT * FROM table1 WHERE row1 $str";

    --
    ==================
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    net
    ==================
    Jerry Guest

  3. #3

    Default Re: Multiple query variables based on str_word_count

    Jerry Stuckle wrote: 
    >
    > Don't bother extracting. Just build your string in a foreach loop, i.e.
    >
    > $str = '';
    > foreach ($search as $s) {
    > if ($str != '')
    > $str .= ' OR ';
    > $.str .= "LIKE $s";
    > }
    > $sql = "SELECT * FROM table1 WHERE row1 $str";
    >[/ref]
    This is what ended up working:

    $table=array(
    'table.column',
    'table.column2',
    etc...
    );

    $str = '';
    foreach ($search as $s) {
    foreach($table as $t){
    if ($str != "")
    $str .= "OR\n";
    $str.= "$t\n" ;
    $str .= "LIKE '%$s%'\n";
    }}
    $q = "
    SELECT
    $alias
    FROM table1
    LEFT JOIN table2
    ON table1.id = table2.id
    WHERE
    $str
    ";

    Thanks for pointing me in the right direction.

    --

    Regards,

    Jeff Gardner
    ___________________________

    "Contrary to popular belief, Unix is user friendly. It just happens
    to be very selective about who its friends are." --Kyle Hearn
    Jeff Guest

Similar Threads

  1. add new nav tab based on a query
    By Mike Yang in forum Coldfusion Flash Integration
    Replies: 0
    Last Post: June 22nd, 02:21 AM
  2. CAML Query: Multiple Query Fields Issue
    By Jon F. in forum ASP.NET Web Services
    Replies: 0
    Last Post: May 12th, 08:19 PM
  3. Replies: 1
    Last Post: August 6th, 11:34 PM
  4. IP based session variables...
    By Nathan Baulch in forum ASP.NET Web Services
    Replies: 2
    Last Post: July 19th, 03:08 AM
  5. Web based query tools
    By sunil in forum ASP.NET General
    Replies: 0
    Last Post: June 27th, 04:55 PM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139