Forum Moderators: open
<?php
$searchclass = $_POST['searchclass'];
$classperiod = $_POST['classperiod'];
include 'login.php';
$query = "SELECT name,
period
FROM schedule
WHERE first='$searchclass'AND period='$classperiod' OR second='$searchclass'AND period='$classperiod' OR third='$searchclass'AND period='$classperiod' OR fourth='$searchclass'AND period='$classperiod' ORfifth='$searchclass'AND period='$classperiod' OR sixth='$searchclass'AND period='$classperiod' OR seventh='$searchclass'AND period='$classperiod' OR eighth='$searchclass'AND period='$classperiod'";
$result = mysql_query($query) or die("Couldn't execute query.");
while ($data = mysql_fetch_array($result)){
{
$name = $data['name'];
}
}
{
?>
<html>
<body>
<?php print "$name"; ?><br />
<?php
}
?>
</body>
</html>
Thanks in advance!
SELECT name,
period
FROM schedule
WHERE first='$searchclass'AND period='$classperiod' OR second='$searchclass'AND period='$classperiod' OR third='$searchclass'AND period='$classperiod' OR fourth='$searchclass'AND period='$classperiod' ORfifth='$searchclass'AND period='$classperiod' OR sixth='$searchclass'AND period='$classperiod' OR seventh='$searchclass'AND period='$classperiod' OR eighth='$searchclass'AND period='$classperiod';
Look at my bolded quotes, are these typos?
EDIT: It's not this board's software, tested in my reply.
You need a SPACE before each and, and there's one OR in there without one.
Second, you might consider using some brackets. It's very likely that even if it did run, you wouldn't get the results you expect. Is it clear (to mysql) what result you're selecting? Using just the first volley of conditions,
WHERE first='$searchclass' AND period='$classperiod' OR second='$searchclass'
So if $first='$searchclass' and period='$classperiod' it will return a result. But it will also return a result if that is not true and $second='$searchclass'. Or is it . . . . if first matches AND period or second matches?
These two scenarios are clarified by
1. WHERE first='$searchclass' AND (period='$classperiod' OR second='$searchclass')
Both the first field and the bracketed condition MUST match, designated by AND. In the bracketed set, one or the other can match, but it HAS to be one or the other, plus the first, or the entire statement returns false.
2. WHERE (first='$searchclass' AND period='$classperiod') OR second='$searchclass'
Both of the first two conditions MUST match, or just the third.
This can be further complicated if you wish to have some of these nested within others, like,
((a=1 and b=2) or c=3) OR (d=4 and (e=5 or f=6 or g=7) and h=8)
This cascades throughout your select and would probably be better served by a union or join, but in this case - bracket that baby so it's clear and you get what you expect.
<?php
$searchclass = $_POST['searchclass'];
$classperiod = $_POST['classperiod'];
include 'login.php';
$query = "SELECT name,
period
FROM schedule
WHERE ((first='$searchclass' AND period='$classperiod') OR (second='$searchclass' AND period='$classperiod') OR (third='$searchclass' AND period='$classperiod') OR (fourth='$searchclass' AND period='$classperiod') OR (fifth='$searchclass' AND period='$classperiod') OR (sixth='$searchclass' AND period='$classperiod') OR (seventh='$searchclass' AND period='$classperiod') OR (eighth='$searchclass'AND period='$classperiod'))";
$result = mysql_query($query) or die("Couldn't execute query.");
while ($data = mysql_fetch_array($result)){
{
$name = $data['name'];
}
}
{
?>
<html>
<body>
<?php print "$name"; ?><br />
<?php
}
?>
</body>
</html>
So i am still getting the "Couldn't execute query" error even after using your suggestions rocknbil. Also, wheelie: not really sure what that was supposed to do but I did it anyway and of course it just returned "SELECT name, period FROM schedule WHERE (first='us history' AND period='1') OR (second='us history' AND period='1') OR (third='us history' AND period='1') OR (fourth='us history' AND period='1') OR (fifth='us history' AND period='1') OR (sixth='us history' AND period='1') OR (seventh='us history' AND period='1') OR (eighth='us history' AND period='1')"