dbTalk Databases Forums  

Multiple query variables based on str_word_count

comp.databases.mysql comp.databases.mysql


Discuss Multiple query variables based on str_word_count in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Jeff Gardner
 
Posts: n/a

Default Multiple query variables based on str_word_count - 09-25-2006 , 09:34 AM






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

Reply With Quote
  #2  
Old   
Jerry Stuckle
 
Posts: n/a

Default Re: Multiple query variables based on str_word_count - 09-25-2006 , 12:18 PM






Jeff Gardner wrote:
Quote:
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.

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.
jstucklex (AT) attglobal (DOT) net
==================


Reply With Quote
  #3  
Old   
Jeff Gardner
 
Posts: n/a

Default Re: Multiple query variables based on str_word_count - 09-26-2006 , 11:29 PM



Jerry Stuckle wrote:
Quote:
Jeff Gardner wrote:
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.


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";

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


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.