This is my first attempt at creating a website using PHP. My database is done in MySQL. It is for a made up university. I am having trouble creating the page that should allow current students enrolled at the university to search for courses and add them to their schedule. So far, I have the page load up correctly and display the form for the student to enter in the search information. However, whenever a subject is chosen, and the search button clicked, I get an error. I don't know what is causing it so I am not able to finish my page. I'm pretty sure I also have other problems also.

The error message I get is "Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in <file name> on line 210"

I would appreciate it if someone would take the time to look through my code and help me find the problem, and point out any other major problems I might have. If that isn't possible, are there any good tutorials available online that could help me? I've searched google and couldn't find anything useful.

My code is below, I also attached a copy of the .php file

<? //this page allows a user to search for courses being offered.
	require("session.php"); //include the script that handles the session information ?>

<?	$level = $_SESSION["level"];//get the user's level from the session table
	$un = $_SESSION["user"];//get the user's username from the session table
	
/////check to make sure the user is logged in as a student. If not, re-direct them to login page.////////////////////////////	
	if($level != -1)
	{
		print "<script>";
		print " self.location='https://205.153.60.235/login.php?unauth'";
		print "</script>";
	}
	else
	{
//////connect to the database///////////////////////////////////////////////////////////////////////////////////////////////
    	$usr = "";//database username
    	$pwd = "";//database password
    	$db = "";//database name
    	$host = "localhost";

    	mysql_connect($host,$usr,$pwd);
    	@mysql_select_db ($db) or die( "Unable to select database");?>

<? ///// begin html code to display information on the page///////////////////////////////////////////////////////////////// ?>

		<HTML>
 			<HEAD>
    			<TITLE>Course Search</TITLE>
				<? //include the created template for the layout of the links/header across left and top of page. ?>
				<LINK REL=StyleSheet HREF="template.css" TYPE="text/css">
			</HEAD>
 			<BODY>
 				<div id="mastHead">
    			<?php include("header\University-Header.html"); //include the header at the top of the page.?>
    			</div><? //close mastHead tag
////////////////links that are available from the current page////////////////////////////////////////////////////////////// ?>
				<div id="linkIndex">
	  				<div align="center">
		  				<p><a href="https://205.153.60.235/login.php">Login/Logout</a></p>
		  				<p><a href="https://205.153.60.235/ViewCurrentStudentInfo.php">View Personal Information</a></p>
		  				<p><a href="https://205.153.60.235/CourseSearch.php">Search for Classes</a></p>
		  				<p><a href="https://205.153.60.235/ViewClassSchedule.php">View Schedule</a></p>
		  				<p><a href="https://205.153.60.235/ViewTuitionInfo.php">Pay Bill</a></p>
		  				<p><a href="https://205.153.60.235/ViewStudentTranscript.php">View Transcript</a></p>
		  				<p><a href="https://205.153.60.235/ViewExamSchedule.php">View Exam Schedule</a><br><br>
		    			<a href="#"></a><br><br>
		    			<a href="#"></a><br><br><br>
   						</p>
	  				</div><? //close center alignment div tag ?>
 				</div> <? //close linkIndex div tag ?>

    
				<div id="contentsBox"><!--Put all of your code in this div tag.-->
				<div align="center">
				<? 		
					if (isset($_POST['ClassSearch'])) {
						$username="";//database username
						$password="";//database password
						$database="";//database name
						mysql_connect(localhost,$username,$password);
						@mysql_select_db($database) or die( "Unable to select database");	
						
						#set variables
						$Subject= $_POST['Subject'];
						$Section=$_POST['Section'];
						$facid=$_POST['facid'];
						$CourseTitle=$_POST['CourseTitle'];
						$CourseNum=$_POST['CourseNum'];
	
/////statements that check the fields to make sure atleast a subject is entered, and searches according to other criteria.////////
					if($Subject == " ")
					{
						print "<SCRIPT>window.alert('Sorry, you must select a subject.')</SCRIPT>";
						print "<script>";
						print " self.location='https://205.153.60.235/newCourseSearch.php'";
						print "</script>"; 
					}//end if
					else if (($Subject != " ") && ($Section != " ") && ($facid != "") && ($CourseTitle != "") && ($CourseNum != ""))
					{// setup SQL statement to retrieve the class search results.
	       				$SQL = "SELECT * 
		   		   				FROM university.classes, university.classsections
				   				WHERE Subject = '$Subject'
				   				AND Section = '$Section'
				   				AND facid LIKE '%$facid'
				   				AND CourseTitle LIKE '%$CourseTitle'
				   				AND CourseNum = '$CourseNum'";
					}
					else if (($Subject != " ") && ($Section != " ") && ($facid != "") && ($CourseTitle != "") && ($CourseNum == ""))
					{// setup SQL statement to retrieve the class search results.
	       				$SQL = "SELECT * 
		   		   				FROM university.classes, university.classsections
				   				WHERE Subject = '$Subject'
				   				AND Section = '$Section'
				   				AND facid LIKE '%$facid'
				   				AND CourseTitle LIKE '%$CourseTitle'";
					}
					else if (($Subject != " ") && ($Section != " ") && ($facid != "") && ($CourseTitle == "") && ($CourseNum != ""))
					{// setup SQL statement to retrieve the class search results.
	       				$SQL = "SELECT * 
		   		   				FROM university.classes, university.classsections
				   				WHERE Subject = '$Subject'
				   				AND Section = '$Section'
				   				AND facid LIKE '%$facid'
				   				AND CourseNum = '$CourseNum'";
					}
					else if (($Subject != " ") && ($Section != " ") && ($facid == "") && ($CourseTitle != "") && ($CourseNum != ""))
					{// setup SQL statement to retrieve the class search results.
	       				$SQL = "SELECT * 
		   		   				FROM university.classes, university.classsections
				   				WHERE Subject = '$Subject'
				   				AND Section = '$Section'
				   				AND CourseTitle LIKE '%$CourseTitle'
				   				AND CourseNum = '$CourseNum'";
					}
					else if (($Subject != " ") && ($Section == " ") && ($facid != "") && ($CourseTitle != "") && ($CourseNum != ""))
					{// setup SQL statement to retrieve the class search results.
	       				$SQL = "SELECT * 
		   		   				FROM university.classes, university.classsections
				   				WHERE Subject = '$Subject'
				   				AND facid LIKE '%$facid'
				   				AND CourseTitle LIKE '%$CourseTitle'
				   				AND CourseNum = '$CourseNum'";
					}
					else if (($Subject != " ") && ($Section != " ") && ($facid != "") && ($CourseTitle == "") && ($CourseNum == ""))
					{// setup SQL statement to retrieve the class search results.
	       				$SQL = "SELECT * 
		   		   				FROM university.classes, university.classsections
				   				WHERE Subject = '$Subject'
				   				AND Section = '$Section'
				   				AND facid LIKE '%$facid'";
					}	
					else if (($Subject != " ") && ($Section != " ") && ($facid == "") && ($CourseTitle != "") && ($CourseNum == ""))
					{// setup SQL statement to retrieve the class search results.
	       				$SQL = "SELECT * 
		   		   				FROM university.classes, university.classsections
				   				WHERE Subject = '$Subject'
				   				AND Section = '$Section'
				   				AND CourseTitle LIKE '%$CourseTitle'";
					}
					else if (($Subject != "") && ($Section != "") && ($facid == "") && ($CourseTitle == "") && ($CourseNum != ""))
					{// setup SQL statement to retrieve the class search results.
	       				$SQL = "SELECT * 
		   		   				FROM university.classes, university.classsections
				   				WHERE Subject = '$Subject'
				   				AND Section = '$Section'
				   				AND CourseNum = '$CourseNum'";
					}	
					else if (($Subject != " ") && ($Section == " ") && ($facid != "") && ($CourseTitle != "") && ($CourseNum == ""))
					{// setup SQL statement to retrieve the class search results.
	       				$SQL = "SELECT * 
		   		   				FROM university.classes, university.classsections
				   				WHERE Subject = '$Subject'
				   				AND facid LIKE '%$facid'
				   				AND CourseTitle LIKE '%$CourseTitle'";
					}
					else if (($Subject != " ") && ($Section == " ") && ($facid != "") && ($CourseTitle == "") && ($CourseNum != ""))
					{// setup SQL statement to retrieve the class search results.
	       				$SQL = "SELECT * 
		   		   				FROM university.classes, university.classsections
				   				WHERE Subject = '$Subject'
				   				AND facid LIKE '%$facid'
				   				AND CourseNum = '$CourseNum'";
					}
					else if (($Subject != " ") && ($Section == " ") && ($facid == "") && ($CourseTitle != "") && ($CourseNum != ""))
					{// setup SQL statement to retrieve the class search results.
	       				$SQL = "SELECT * 
		   		   				FROM university.classes, university.classsections
				   				WHERE Subject = '$Subject'
				   				AND CourseTitle LIKE '%$CourseTitle'
				   				AND CourseNum = '$CourseNum'";
					}
					else if (($Subject != " ") && ($Section != " ") && ($facid == "") && ($CourseTitle == "") && ($CourseNum == ""))
					{// setup SQL statement to retrieve the class search results.
	       				$SQL = "SELECT * 
		   		   				FROM university.classes, university.classsections
				   				WHERE Subject = '$Subject'
				   				AND Section = '$Section'";
					}
					else if (($Subject != " ") && ($Section == " ") && ($facid != "") && ($CourseTitle == "") && ($CourseNum == ""))
					{// setup SQL statement to retrieve the class search results.
	       				$SQL = "SELECT * 
		   		   				FROM university.classes, university.classsections
				   				WHERE Subject = '$Subject'
				   				AND facid LIKE '%$facid'";
					}
					else if (($Subject != " ") && ($Section == " ") && ($facid == "") && ($CourseTitle != "") && ($CourseNum == ""))
					{// setup SQL statement to retrieve the class search results.
	       				$SQL = "SELECT * 
		   		   				FROM university.classes, university.classsections
				   				WHERE Subject = '$Subject'
				   				AND CourseTitle LIKE '%$CourseTitle'";
					}
					else if (($Subject != " ") && ($Section == " ") && ($facid == "") && ($CourseTitle == "") && ($CourseNum != ""))
					{// setup SQL statement to retrieve the class search results.
	       				$SQL = "SELECT * 
		   		   				FROM university.classes, university.classsections
				   				WHERE Subject = '$Subject'
				   				AND CourseNum = '$CourseNum'";
					}
					else if (($Subject != " ") && ($Section == " ") && ($facid == "") && ($CourseTitle == "") && ($CourseNum == ""))
					{// setup SQL statement to retrieve the class search results.
	       				$SQL = "SELECT * 
		   		   				FROM university.classes, university.classsections
				   				WHERE Subject = '$Subject'";
					}
	
					// execute mysql statement		   
					$result=mysql_query($query);
					$num=mysql_num_rows($result); 
					mysql_close();

					//check to see if there are no results
					if ($num == 0) 
					{
					echo "<center>I am sorry, but there are no classes being offered that match what you are searching for.
						  <br>Please try searching for something else.</center>";
					}
					
					$i = 0;//initialize i at 0.
					while ($i < $num) 
					{//loops through results until i is no longer less than num.
						$Subject=mysql_result($result,$i,"Subject");
						$Section=mysql_result($result,$i,"Section");
						$facid=mysql_result($result,$i,"facid");
						$CourseTitle=mysql_result($result,$i,"CourseTitle");
						$CourseNum=mysql_result($result,$i,"CourseNum");
						$MeetingTimes=mysql_result($result,$i,"MeetingTImes");
						$Days=mysql_result($result,$i,"Days");
			?>		    
			<!---form to display the class information and allow the student to add it to their schedule if they choose to---> 
							<form id="form1" name="form1" method="post" action="">
  							<table width="336" height="206" border="0" cellpadding="0" cellspacing="0">
    						<tr>
      							<td width="124"><div align="right">Subject:</div></td>
      							<td width="212"><label><?php print $HTTP_GET_VARS['Subject']; ?></label>&nbsp;</td>
    						</tr>
    						<tr>
      							<td><div align="right">Section:</div></td>
      							<td><label><?php print $HTTP_GET_VARS['Section']; ?></label>&nbsp;</td>
    						</tr>
    						<tr>
      							<td><div align="right">Instructor:</div></td>
      							<td><label><?php print $HTTP_GET_VARS['facid']; ?></label>&nbsp;</td>
    						</tr>
    						<tr>
      							<td><div align="right">Course Title: </div></td>
      							<td><label><?php print $HTTP_GET_VARS['Title']; ?></label>&nbsp;</td>
    						</tr>
    						<tr>
      							<td><div align="right">Meeting Times: </div></td>
      							<td><label><?php print $HTTP_GET_VARS['MeetingTimes']; print " "; print $HTTP_GET_VARS['Days']; ?></label>&nbsp;</td>
    						</tr>
    						<tr>
      							<td><div align="right">Course Number: </div></td>
      							<td><label><?php print $HTTP_GET_VARS['CourseNum'];?></label>&nbsp;</td>
    						</tr>
  						</table>
  						<p>
    					<INPUT TYPE="BUTTON" VALUE="Add Class" ONCLICK="window.location.href='https://205.153.60.235/StudentAddClass.php?Subject=<?php echo "$Subject";?>&Section=<?php echo "$Section";?>&facid=<?php echo "$facid";?>&Title=<?php echo "$Title";?>&MeetingTimes=<?php echo "$MeetingTimes";?>&CourseNum=<?php echo "$CourseNum";?>'" />
  						</p>
						</form>;
				<? 	$i++;
					} 
				?> </table></center> <?
				}
				else 
				{?>
			<!------- form to select what information you want to base your search on-------->
  					<form id="form" name="form" method="POST" action="<? $ECHO_SELF?>">
					<p>
          			<label><span class="style2">Search for Courses:</span></label>
          			<br />
        			Use the selection options to search for a course you would like to add to your schedule. 
					You may choose any combination of fields to narrow your  search, but you must select at least one Subject. 
					Click  Search  when your selection is complete.</p>
        			<table width="599" height="226" border="0" cellpadding="0" cellspacing="0">
          				<tr>
            				<td width="119" height="202" valign="top">
								<p><strong>Subject:</strong></p>
              					<p><strong>Course Number:</strong></p>
              					<p><strong>Section:</strong></p>
              					<p><strong>Course Title:</strong></p>
              					<p><strong>Instructor: </strong></p></td>
            				<td width="480" valign="top"><p>
              					<select name="Subject" size="1" id="Subject">
                					<option value=" " selected="selected"></option>
                					<option value="ENGL">ENGL</option>
                					<option value="MATH">MATH</option>
                					<option value="CIS">CIS</option>
                					<option value="PHYS">PHYS</option>
                					<option value="CHEM">CHEM</option>
                					<option value="PEES">PEES</option>
                					<option value="PHIL">PHIL</option>
              					</select>
              			<br />
              			<br />
              					<input name="CourseNum" type="text" id="CourseNum" maxlength="3" />
            					</p>
              					<p>
                				<select name="Section" size="1" id="Section">
                  					<option value=" " selected="selected"> </option>
                  					<option value="1">1</option>
                  					<option value="2">2</option>
                  					<option value="3">3</option>
                  					<option value="4">4</option>
                  					<option value="5">5</option>
                  					<option value="6">6</option>
                  					<option value="7">7</option>
                  					<option value="8">8</option>
                  					<option value="9">9</option>
                  					<option value="10">10</option>
                  					<option value="11">11</option>
                  					<option value="12">12</option>
                  					<option value="13">13</option>
                  					<option value="14">14</option>
                  					<option value="15">15</option>
                  					<option value="16">16</option>
                  					<option value="17">17</option>
                  					<option value="18">18</option>
                  					<option value="19">19</option>
                  					<option value="20">20</option>
                  					<option value="21">21</option>
                  					<option value="22">22</option>
                  					<option value="23">23</option>
                  					<option value="25">25</option>
                				</select>
              					</p>
              					<p>
                				<input name="CourseTitle" type="text" id="CourseTitle" maxlength="25" />
              					</p>
              					<p>
                				<input name="facid" type="text" id="facid" maxlength="15" />
              					</p></td>
          				</tr>
          				<tr>
            				<td height="24" colspan="2" align="left" valign="top"><p>
              					<input name="ClassSearch" type="submit" id="ClassSearch" value="Search" /> 
                   				<input type="reset" name="Reset" value="Reset" />
            					</p>
							</td>
            			</tr>
        			</table>
        			<p>
          			<label></label>
        			</p>
					</form> 
<? 					} 
				}
?></div></div>
			</HTML>

Recommended Answers

All 4 Replies

when i look at your example page in dreamweaver I noticed that on your first form you didnt have an action set this could cause problems I also noticed that dreamweaver doesnt recognize the fact that you have a database set in it you might want to relook your database and make sure that it is set right

plz explain it in short .............

Your problem is not with the mysql_num_rows it is with the line before it the mysql_query line.

Your query line looks like this:
$result=mysql_query($query);

However you did not define the $query variable. Technicaly the error is at the query, but because it is not a serious error the code continues to go.

You place your querys in a variable named $SQL

You should replace either the $SQLs with $query or Replace $query with $SQL.

ie.
$result=mysql_query($SQL);

As well as a good habit you should put error catching on your queries to ensure they process properly like so:

$result=mysql_query($SQL) or die("Unable to process database query. Please contact the admin: " . mysql_error());

With this in place if there is an error with the query it will stop processing code, and display the error message above along with the error message from mySql

Thanks, I will try that and see iif I can fix my code from there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.