Re: Fetching 1 array from either one of 2 possible columns?
| From: | Thalis A. Kalfigopoulos | Date: | Fri, 22 Mar 2002 04:43:09 +0000 |
| Subject: | Re: Fetching 1 array from either one of 2 possible columns? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-89647@lists.php.net to get a copy of this message | ||
The problem is in your SQL query.
Try it as:
$sql = "SELECT * FROM links WHERE (SUBJECT1='$subject' OR
SUBJECT2='$subject') AND GEOGRAPHIC='$geographic' ORDER BY ORGANIZATION
ASC";
The AND operator has higher precendence than the OR, so given that you didn't have parenthesis,
it was interpreted as SUBEJCT1='$subejct' OR (SUBEJCT2='$subject' AND
GEOGRAPHIC='$geographic')
and I'd bet that in the example you ran $geographic was not equal to 'Brazil'
otherwise you'd have gotten a hit on the second row as well.
cheers,
thalis
On Thu, 14 Mar 2002, Laurie Landry wrote:
> I'm working on a query by selection type of form, where if a user
> selects a subject to get information. Each database entry will have 2
> subject fields, Subject 1 being the main subject and Subject 2 being the
> cross-subject. A table is set up like this:
>
> +------+--------------+------+----------+----------+-------------+
> | ID | Organization | URL | SUBJECT1 | SUBJECT2 | Geographic |
> +------+--------------+------+----------+----------+-------------+
> | 1 | Acme | www | Math | English | Canada |
> | 2 | Loony Toons | www | Comedy | Math | Brazil |
>
> ...
>
>
> The idea is that the query will check the database to see if $Subject
> has a match in either Subject1 or Subject2. the geographic is an
> optional selection. If I select Math as a subject, and left the
> Geographic option unselected, I want it to go into either Subject1 and
> Subject2 to find Math. In this case both records would be a hit.
>
> Below is my query setup and formatting:
> *******
> $sql = "SELECT * FROM links WHERE SUBJECT1='$subject' OR
> SUBJECT2='$subject' AND GEOGRAPHIC='$geographic'
> ORDER BY ORGANIZATION ASC";
>
> $sql_result = mysql_query($sql);
> if (!$sql_result) {
> echo "Can't execute $sql " . mysql_error();
> exit;
> }
>
> // organizes data in an orderly manner (ie bulleted area)
> while ($row = mysql_fetch_array($sql_result)) {
>
> $esc_organization = $row["ORGANIZATION"];
> $esc_desc = $row["DESCRIPTION"];
> $esc_url = $row["URL"];
> $esc_subject = $row["SUBJECT1"];
> $esc_geographic = $row["GEOGRAPHIC"];
>
> $organization = stripslashes($esc_organization);
> $description = stripslashes($esc_desc);
> $url = stripslashes($esc_url);
> $subject = stripslashes($esc_subject);
> $geographic = stripslashes($esc_geographic);
>
> $option_block .= "
> <li>
> <a
> href=\"http://$url\">$organization</a></li><br>
> $description<br>
> URL: <a
> href=\"http://$url\">$url</a></li>\n";
> }
> ********
> Now, of course, if I were to use this, it will only use the Subject1
> data. How do I tell it to use the results from either Subject 1 or
> Subject 2?
>
> Thanks in advance,
>
> Laurie M. Landry
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>