-1

I'm working on a website for educational use, and I've run in to a little error when I've tried to build a file that does some logic.

Basically, the code is meant to get 3 data values, if only the first bit of data is present, it runs x query, if first and second are present it runs y query, and so on, and so on, until it runs 8 tests on the GET data. For some background info, level = a class of study level (IGCSE, IB, A-Levels), Subject is a subject (Math, English, Science), School is the name of the school.

The database structure is as followed:

'id' INT(11)
'title' VARCHAR(255)
'description' VARCHAR(1000)
'filename' VARCHAR(255)
'uploader' VARCHAR(255)
'school' VARCHAR(255)
'subject' VARCHAR(255)
'level' VARCHAR(255)
'downloads' VARCHAR(20)
'views' VARCHAR(20)
'helpful' INT(11)
'nothelpful' INT(11)
'date' DATE

And I'm running the following PHP code:

<?
/*
YYY
YNY
YNN
YYN
NNN
NYN
NNY
NYY
*/
/* DECLARE VARS */
$subject    =           $_GET['subject'];
$level      =           $_GET['lvl'];
$school     =           $_GET['school'];
include("connect.php");

if($subject!="" && $level!="" && $school!=""){
/* GET Notes that have x subject, y level, and z school */
$yyyq = mysql_query("SELECT * FROM noteshare_notes WHERE subject='$subject' AND level='$level' AND school='$school' ORDER BY helpful DESC LIMIT 30"); /* quote missed here */
while($row=mysql_fetch_array($yyyq)){
echo '<span id="topic">[<a href="list.php?subject='.$row['subject'].'&lvl='.$row['level'].'">'.$row['level'].' '.$row['subject'].'</a>]</span> <a href="note.php?id='.$row['id'].'">'.$row['title'].'</a> <span id="usersize"><i>[uploaded by <a href="profile.php?id='.$row['uploader'].'">'.$row['uploader'].'</a>]</i></span><br>'; }
//yyy
}

if($subject!="" && $level!="" && $school==""){
/* Get Notes from x Subject and X Level, regardless of school. */
$yynq = mysql_query("SELECT * FROM noteshare_notes WHERE subject='$subject' AND level='$level' ORDER BY helpful DESC LIMIT 30"); /* quote missed here */
while($row=mysql_fetch_array($yynq)){
echo '<span id="topic">[<a href="list.php?subject='.$row['subject'].'&lvl='.$row['level'].'">'.$row['level'].' '.$row['subject'].'</a>]</span> <a href="note.php?id='.$row['id'].'">'.$row['title'].'</a> <span id="usersize"><i>[uploaded by <a href="profile.php?id='.$row['uploader'].'">'.$row['uploader'].'</a>]</i></span><br>'; }
//yyn
}

if($subject!="" && $level=="" && $school==""){
/* Get Level before Getting Notes without School */
$ynnq = mysql_query("SELECT * FROM noteshare_notes WHERE subject='$subject' GROUP BY level");
echo "<center>";
while($row=mysql_fetch_array($ynnq)){
echo '<a href="list.php?lvl='.$row['level'].'&subject='.$row['subject'].'">'.$row['level'].'</a><br>'; }
//ynn 
}

if($subject!="" && $level=="" && $school!=""){
/* Get level based on school + subject */
$ynyq = mysql_query("SELECT * FROM noteshare_notes WHERE subject='$subject' AND school='$school' GROUP BY level");
while($row=mysql_fetch_array($ynyq)){
echo '<a href="list.php?lvl='.$row['level'].'&school='.$row['school'].'&subject='.$row['subject'].'">'.$row['level'].'</a><br>'; }
//yny
}

if($subject=="" && $level=="" && $school==""){
//Error - Nothing + Nothing + Nothing = Nothing
//nnn
}

if($subject=="" && $level=="" && $school!=""){
/* Get Subject First based on School, thus redirecting to previous ynn (reverse clause)*/
$nnyq = mysql_query("SELECT * FROM noteshare_notes WHERE school='$school' GROUP BY subject");
while($row=mysql_fetch_array($nnyq)){
echo '<a href="list.php?subject='.$row['subject'].'&school='.$row['school'].'">'.$row['subject'].'</a><br>'; }
//nny
}

if($subject=="" && $level!="" && $school!=""){
/* Get Subjects based on level + school */
$nyyq = mysql_query("SELECT * FROM noteshare_notes WHERE level='$level' AND school='$school' GROUP BY subject");
while($row=mysql_fetch_array($nyyq)){
echo '<a href="list.php?subject='.$row['subject'].'&lvl='.$row['level'].'&school='.$row['school'].'">'.$row['subject'].'</a><br>'; }
//nyy
}

if($subject=="" && $level!="" && $school==""){
/* Get Subject based on level */
$nynq = mysql_query("SELECT * FROM noteshare_notes WHERE level='$level' GROUP BY subject");
while($row=mysql_fetch_array($nyyq)){
echo '<a href="list.php?subject='.$row['subject'].'&lvl='.$row['level'].'">'.$row['subject'].'</a><br>'; }
//nyn
}
?>

Apologies as it's amassed with comments and probability, lol.

It keeps returning me with the

mysql_fetch_array(): supplied argument is not a valid MySQL result resource

error, and I have no clue how to fix it, I've tried lots, and lots of things to fix, nothing works. I think it might be due to the multiple ANDs or the fact that there aren't 30 rows in the database

Would anyone know how to fix this? Any answers will be very helpful :)

Cheers!

6
  • Which case throws the error? Or do all of them result in an error? Commented Feb 19, 2012 at 12:18
  • Put the following behind your mysql_query, plz: if( mysql_errno() ) { echo mysql_error(); } and post the output it generates. Commented Feb 19, 2012 at 12:18
  • No error showing @Sirko :( the entire page dies. Commented Feb 19, 2012 at 12:29
  • And @Chris, it looks to be all of them :S Commented Feb 19, 2012 at 12:29
  • I'd guess that the error is in the connect.php then. Are you sure that the mysql connection is established, after all? Commented Feb 19, 2012 at 12:32

1 Answer 1

1

Give these 2 a try. You had single quotes in strange places. You had no single quote after $school, and in both cases a single quote after LIMIT 30. These would probably cause MySQL errors, or at the least lead to unexpected rows being selected.

$yyyq = mysql_query("SELECT * FROM noteshare_notes WHERE subject='$subject' AND level='$level' AND school='$school' ORDER BY helpful DESC LIMIT 30");

$yynq = mysql_query("SELECT * FROM noteshare_notes WHERE subject='$subject' AND level='$level' ORDER BY helpful DESC LIMIT 30");
Sign up to request clarification or add additional context in comments.

3 Comments

Ah, Cheers Simon - That and one more little correction seemed to do it :) Thank you, very, very much!
It's always nice when it's a simple answer eh ;) :)
very ;) 15char. Though I think 'xchar' is against SO TOS, so, hi :D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.