Re: HOw to modify a php database query from general to specific?
| From: | Anthony | Date: | Fri, 23 May 2003 13:42:20 +0000 |
| Subject: | Re: HOw to modify a php database query from general to specific? | ||
| References: | 1 | Groups: | php.windows |
| Request: | Send a blank email to php-windows+get-20063@lists.php.net to get a copy of this message | ||
You just have to do some checking on your form data and build two queries.
I do it like this
$query= 'select * from TABLE1 where FIELD2 = donuts ';
if (isset($_GET['somefield']
$query.=" AND FIELD3 ='{$_GET['somefield']}'";
One thing to remember is that you can't check regular form data input fields
for "" becasue if they are blank, many browsers don't send them at all. So
saying something like if ($_GET['somefield']!="") would give you a warning
if it was blank since teh variable does not exist. Of, and when concating
sql this way, don;t forget to put a space between the " and the AND, that
always screws me up for a few mins. Hope this helps.
- Anthony
"Acorn Tutors" <info@acorntutors.com> wrote in message
news:20030522120718.29500.qmail@pb1.pair.com...
> Hi folks,
>
> I have a database query that I wanted to modify. If a person wanted to
> search for any item in a field (i.ie. NO Preference in a Web page form),
but
> only certain items in another field, how would I set up the php code to do
> this
>
> Would it be something like this?
>
> select * from TABLE1 where FIELD2 = donuts AND FIELD3 = ????
>
>
> What would I put in the ???? to indicate that I wanted to select
everything
> from FIELD3. I ask this because if the user actually selects an item for
the
> FIELD3 query (as opposed to no preference), the query must be able to
allow
> for specific searches as well as being able to search all. In other words,
> are there any wildcards that can be used to easily make it so that the
query
> can be as broad as any row in FIELD3 or as specific as single rows in
> FIELD3.
>
> Help appreciated,
> Doug
>
>