Jump to content

[SOLVED] including variables in a query EVEN if they're empty


glennn.php

Recommended Posts

ok, sorry for my ignorance, but i'm curious about whether to do one of two things:

 

Given a form with about 10 choices, many (all) of which might return an array -

 

$offering_sql might contain a few values, or it might contain none. if it contains none, then this selects ALL the values in that table field, i'm pretty sure that's obvious (not sure of anything today).

 

my query might be built like so:

SELECT fice FROM characteristics WHERE $offering_sql AND $public_private_sql AND $variable AND ...

 

 

My question is, considering that some values will be empty, is it better to do this:

if ($offerings != '') {
$offering_sql = "offering IN ($offering_sql)";
} else {
if ($offerings == '') {
$offering_sql = "offering LIKE '%'";
}

 

or this:

if ($offerings != '') {
$offering_sql = "offering IN ($offering_sql) AND";
} else {
if ($offerings == '') {
$offering_sql = "";
}

 

in other words, IF ONLY ONE FORM CATEGORY OF TEN IS CONTAINS VALUES, should my query be:

SELECT fice FROM characteristics WHERE offering LIKE '%' AND public_private LIKE '%'  AND (value) LIKE '%'  AND (value) LIKE '%'  AND (value) LIKE '%'  AND (value) LIKE '%' ...

 

or should i configure those if statements to return '' if i can... does it matter either way?

 

don't know what to consider while considering these choices...

 

thanks for anyone's help,

GN

 

 

 

Link to comment
Share on other sites

try

$where = array();
if ($offerings != '') {
$where[] = "offering IN ($offering_sql)";
} 
if ($some_field != '') {
$where[] = "some_field IN ($some_field_sql)";
} 
//etc.
$where = count($where) ? implode(' AND ', $where) : 1;
$sql = "SELECT fice FROM characteristics WHERE $where";

Link to comment
Share on other sites

this returns

 

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/coburncr/public_html/hepinc/admin_characteristics.php on line 405

 

 

this is returning no values no matter how many options are chosen in the form. "implode(',',$offering);" would be something like 03,04,05

$offering = $_POST['highest_deg'];
if ($offering != '') {
$offerings = implode(',',$offering);
} else {
if ($offering == '') {
$offerings = '';
}
}

$where = array();
if ($offerings != '') {
$where[] = "offering IN ($offering_sql)";
} 

$where = count($where) ? implode(' AND ', $where) : 1;


 

 

 

$where holds "offering IN ()" no matter what. i'm trying to get it to EITHER have "offering IN (03,04,05) AND" OR ""...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.