So I've been figuring this out for an hour now and I can't seem to find the problem is so i have this code:

$QUERYsubject="select * from tbltablestudent where S_ID='".$myusername."'";
	$subject=mysql_query($QUERYsubject) or die(mysql_error());
	$QUERYgr1="Select * from ".$subject[Subject].", tbltablestudent where ".$subject[Subject].".S_ID=tbltablestudent.S_ID and tbltablestudent.S_ID='".$myusername."' and Grading_period='1st_Grading'";
	$gr1=mysql_query($QUERYgr1) or die(mysql_error());

and I am getting this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' tbltablestudent where .S_ID=tbltablestudent.S_ID and tbltablestudent.S_ID='sh01' at line 1

-----
Please help me!

Recommended Answers

All 6 Replies

It looks like you are trying to reference the result of the first query in the second but without converting the first query itto an array first. Maybe try this for line 2

$subject=mysql_fetch_assoc(mysql_query($QUERYsubject)) or die(mysql_error());

also noticed a comma on the second query along with a declaration of 2 tables that you are trying to select from. If you want to query 2 tables at once I think you have to join them. Can you show us your db structure and tell us what you are looking to return?

also noticed a comma on the second query along with a declaration of 2 tables that you are trying to select from. If you want to query 2 tables at once I think you have to join them. Can you show us your db structure and tell us what you are looking to return?

The second query is the actual joining of two tables. the first query is to call for the name of the table our system automatically created and will be assigned in ".$subject[Subject]."

I've attached a screenshot of our database btw~

first one is table tbltablestudent and the second one is the table created and assigned as ".$subject[Subject]." .. i want to retrieve the TOTAL_GRADE btw, but i still get the same error.

this makes use of more PHP to process the request. I am sure there is a way to do this all sql but not that I know of.

<?
      $QUERYsubject=mysql_query("select * from tbltablestudent where S_ID='".$myusername."'")or die (mysql_error());// get a list of all subjects this student took
      //performa query for each subject and assign the result to an array
	  while($entry=mysql_fetch_assoc($QUERYsubject)){
		  $result=mysql_fetch_assoc(mysql_query("SELECT * FROM ".$entry['Subject']." WHERE S_ID='".$myusername."' LIMIT 1")) or die(mysql_error());//query a single subject table at a time
		  $studentGrades[$entry['subject']]=$result;//assigns the result into an associative array
	  }
	  ?>

this makes use of more PHP to process the request. I am sure there is a way to do this all sql but not that I know of.

<?
      $QUERYsubject=mysql_query("select * from tbltablestudent where S_ID='".$myusername."'")or die (mysql_error());// get a list of all subjects this student took
      //performa query for each subject and assign the result to an array
	  while($entry=mysql_fetch_assoc($QUERYsubject)){
		  $result=mysql_fetch_assoc(mysql_query("SELECT * FROM ".$entry['Subject']." WHERE S_ID='".$myusername."' LIMIT 1")) or die(mysql_error());//query a single subject table at a time
		  $studentGrades[$entry['subject']]=$result;//assigns the result into an associative array
	  }
	  ?>

thank you very much! I tried it and it worked. I kinda altered the codes though.. and i got these: (attached)

---
what should i do to only display the total grade for every grading without looping the subject?

echo "<table width='90%' border='1' cellpadding='1' cellspacing='1' bgcolor='#ffffff'>";
		  echo "<tbody><tr bgcolor='#eaeaea' align='center'>";
		  echo "<td width='10%'>Code</td>";
		  echo "<td width='20%'>Description</td>";
		  echo "<td width='10%'>1st</td>";
		  
			      $QUERYsubject=mysql_query("select * from tbltablestudent where S_ID='".$myusername."'")or die (mysql_error());// get a list of all subjects this student took
      //performa query for each subject and assign the result to an array
	  while($entry=mysql_fetch_assoc($QUERYsubject)){
			  	echo "<tr><td>".$entry["Subjcode"];
				echo "<td>".$entry["Subjdesc"];
			$result=mysql_fetch_assoc(mysql_query("SELECT * FROM ".$entry['Subject'].", tbltablestudent where (".$entry['Subject'].".S_ID=tbltablestudent.S_ID) and tbltablestudent.S_ID='".$myusername."' and Grading_Period='2nd_Grading'")) or die(mysql_error());//query a single subject table at a time
			$studentGrades[$entry['Subject']]=$result;//assigns the result into an associative array
		  

			echo "<td>".$result["TOTAL_GRADE"];
			echo "</tbody></table>";
				  }
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.