Jump to content

PHP query to db


odisey

Recommended Posts

I am not sure if this is the right forum:

 

My question is about running a db parse query.  Using PhpMyAdmin I would run something like:

 

SELECT student.studentname, characteristics.char_desc, SUM(student_score.score) as total

FROM student_score

    INNER JOIN question  ON student_score.q_id = question.q_id

    INNER JOIN area ON question.area_id = area.area_id

    INNER JOIN characteristics ON area.char_id = characteristics.char_id

    INNER JOIN student ON student_score.student_id = student.student_id

WHERE student_score.mid_final = 'M'

    AND student_score.student_id = '7001234'

    AND student_score.year = '2006'

GROUP BY student.studentname, characteristics.char_desc

 

 

Notice I have accessed columns in two tables, student and characteristics.  Is it possable to run such a query in Php to parse a MySQL db?

 

Something like

 

    $query = "SELECT column FROM table1 AND column FROM table2, SUM SUM (student_score.score) as total

FROM student_score

    INNER JOIN question  ON student_score.q_id = question.q_id

    INNER JOIN area ON question.area_id = area.area_id

    INNER JOIN characteristics ON area.char_id = characteristics.char_id

    INNER JOIN student ON student_score.student_id = student.student_id

WHERE student_score.mid_final = 'M'

    AND student_score.student_id = '7001234'

    AND student_score.year = '2006'

GROUP BY student.studentname, characteristics.char_desc";

 

 

$result = @mysql_query($query);

   

  if ($result) {

 

 

  while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

 

 

PRINT HERE

 

Link to comment
Share on other sites

You've done all the hard work yourself already.

 

The finishing touch (to see what you are getting) would be to do something like :

while($row ....) {

echo "<pre>";
print_r($row);
echo "</pre>";

}

Please note this is for debugging really, but it'll show you what you need to know.

Link to comment
Share on other sites

I am trying this simple snippet to get a print to build from -- I get nothing...  Any ideas why?

 


 $query = "SELECT student.last_name, characteristics.char_desc

WHERE student_score.mid_final = 'M'
    AND student_score.student_id = '700900127'
    AND student_score.year = '2006'";

/*   

, SUM (student_score.score) as total
FROM student_score 

INNER JOIN question ON student_score.q_id = question.q_id
    INNER JOIN area ON question.area_id = area.area_id
    INNER JOIN characteristics ON area.char_id = characteristics.char_id
    INNER JOIN student ON student_score.student_id = student.student_id

GROUP BY student.last_name, characteristics.char_desc";    */


$result = @mysql_query($query);
          
         if ($result) {
                             echo 'result';
        
      while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
   

	/*	  	  
	   echo '<p align="left">' . $row['total'] . '</p>'; 
	   
	//  ' . $row['char_desc']'</p>'; 	
	   
	   echo '<br /><br />';     

		*/


		echo "<pre>";
print_r($row);
echo "</pre>";
		   } 
		   
	}   



 

 

 

Thank you for all your help,

Marc

Link to comment
Share on other sites

I've narrowed it down to the SELECT statement dot syntax.  Why would dot syntax execute in the WHERE statement and not the SELECT statement?

 

 

This will not execute:

 

	 $query = "SELECT student_score.description_id, student.last_name WHERE student_score.mid_final = 'M' AND student_score.student_id = 700900127 AND student_score.year = 2006 AND student.student_id = 700900127";

 

This will

 

	 $query = "SELECT description_id FROM student_score WHERE student_score.mid_final = 'M' AND student_score.student_id = 700900127 AND student_score.year = 2006";

 

 

Marc

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.