Dynamic SQL query help  
Author Message
john_blakley





PostPosted: 2003-10-18 3:21:28 Top

php-general, Dynamic SQL query help Hi all,

First off, I'm a newbie.

Can someone help? I have been fighting this for 3 days now and can't
figure out what's wrong.

I'm trying to create a SQL query based off of $HTTP_POST_VARS. Here's
the code:

foreach($HTTP_POST_VARS as $key => $value)
{
$sStr=array($key . "=" . $value . ", ");

$search=join(",",$sStr);

echo $search; //echoing here to print $search values.

$query="Select * from employee where"."?".$search;

}

echo $query;

The problem is: I have 7 fields on a search form. I never know what
and in what combination someone will search with. I was wanting to
build a query based off of the values that HTTP_POST_VARS receives. I
can get the full 'query' string up to the echo $search statement. But
2 problems happen. What I want to search on is there, but, when I add
it to the $query= statement, I only get the last value of
HTTP_POST_VARS!!! Nothing else. I'm frustrated. Maybe someone could
help me with the code to be able to build the SQL statement on the
fly. The other thing that happens is my $search=join statement leaves
a comma at the end of the last HTTP_POST_VARS which would (when it
works) create an error in the SQL statement and I need to strip that
last comma out so the statement doesn't fail.

Thanks for your help!
 
john_blakley





PostPosted: 2003-10-20 21:33:00 Top

php-general >> Dynamic SQL query help Hi all,

First off, I'm a newbie.

Can someone help? I have been fighting this for 3 days now and can't
figure out what's wrong.

I'm trying to create a SQL query based off of $HTTP_POST_VARS. Here's
the code:

foreach($HTTP_POST_VARS as $key => $value)
{
$sStr=array($key . "=" . $value . ", ");

$search=join(",",$sStr);

echo $search; //echoing here to print $search values.

$query="Select * from employee where"."?".$search;

}

echo $query;

The problem is: I have 7 fields on a search form. I never know what
and in what combination someone will search with. I was wanting to
build a query based off of the values that HTTP_POST_VARS receives. I
can get the full 'query' string up to the echo $search statement. But
2 problems happen. What I want to search on is there, but, when I add
it to the $query= statement, I only get the last value of
HTTP_POST_VARS!!! Nothing else. I'm frustrated. Maybe someone could
help me with the code to be able to build the SQL statement on the
fly. The other thing that happens is my $search=join statement leaves
a comma at the end of the last HTTP_POST_VARS which would (when it
works) create an error in the SQL statement and I need to strip that
last comma out so the statement doesn't fail.

Thanks for your help!