refiking Posted December 12, 2008 Share Posted December 12, 2008 OK. So, I have a query that works great below: $result = mysql_query("SELECT ph.first_name,ph.last_name,ph.current_school,ph.height,ph.weight,ph.school_commitment, p.rankESPN, p.rankHoopscoop, p.rankHoopmasters,p.rankRivals, p.rankScout FROM players_heap as ph,players as p WHERE p.p_id =ph.p_id AND ph.graduation_year='$class' AND ( (p.rankESPN BETWEEN '$rank_min' AND '$rank_max') OR (p.rankHoopscoop BETWEEN '$rank_min' AND '$rank_max') OR ( p.rankHoopmasters BETWEEN '$rank_min' AND '$rank_max' ) OR (p.rankRivals BETWEEN '$rank_min' AND '$rank_max') OR ( p.rankScout BETWEEN '$rank_min' AND '$rank_max') )GROUP BY ph.p_id ORDER BY p.rankESPN, p.rankHoopmasters,p.rankRivals, p.rankScout ASC "); I am writing a function based on this query that allows me to add/remove features in the query and display. I keep getting this error message: 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 'GROUP BY ph.p_id ORDER BY p.rankESPN ASC' at line 1 Here's the code of the function up until the query: IF ($number_of_ranks == "1"){ $rank1 = "rank" . $rank_systems; $rastring1 = "`p." . $rank1 . "`"; $rastring2 = "(p.$rank1 BETWEEN '$rank_min' AND '$rank_max')"; } ELSE { $rank_systems = explode("-",$rank_systems); $rank1 = 'rank' . $rank_systems[0]; $ra1 = "`p." . $rank1 . "`"; $ra21 = "(p.$rank1 BETWEEN '$rank_min' AND '$rank_max')"; $rank2 = 'rank' . $rank_systems[1]; $ra2 = ", `p." . $rank2 . "`"; $ra22 = " OR (p.$rank2 BETWEEN '$rank_min' AND '$rank_max')"; IF ($number_of_ranks > "2"){ $rank3 = 'rank' . $rank_systems[2]; $ra3 = ", `p." . $rank3 . "`"; $ra23 = " OR (p.$rank3 BETWEEN '$rank_min' AND '$rank_max')"; } IF ($number_of_ranks > "3"){ $rank4 = 'rank' . $rank_systems[3]; $ra4 = ", `p." . $rank4 . "`"; $ra24 = " OR (p.$rank4 BETWEEN '$rank_min' AND '$rank_max')"; } $rastring1 = $ra1 . $ra2 . $ra3 . $ra4 . $ra5; $rastring2 = "( " . $ra21 . $ra22 . $ra23 . $ra24 . " )"; } IF ($number_of_attributes == "1"){ $attribute1 = $attributes; $atstring = "`" . $attribute1 . "`"; } ELSE{ $attributes = explode ("-", $attributes); $attribute1 = '`ph.' . $attributes[0] . "`"; $attribute2 = ', `ph.' . $attributes[1] . "`"; IF ($number_of_attributes > "2"){ $attribute3 = ', `ph.' . $attributes[2] . "`"; } IF ($number_of_attributes > "3"){ $attribute4 = ', `ph.' . $attributes[3] . "`"; } IF ($number_of_attributes > "4"){ $attribute5 = ', `ph.' . $attributes[4] . "`"; } IF ($number_of_attributes > "5"){ $attribute6 = ', `ph.' . $attributes[5] . "`"; } IF ($number_of_attributes > "6"){ $attribute7 = ', `ph.' . $attributes[6] . "`"; } IF ($number_of_attributes > "7"){ $attribute8 = ', `ph.' . $attributes[7] . "`"; } IF ($number_of_attributes > "8"){ $attribute9 = ', `ph.' . $attributes[8] . "`"; } IF ($number_of_attributes > "9"){ $attribute10 = ', `ph.' . $attributes[9] . "`"; } $atstring = $attribute1 . $attribute2 . $attribute3 . $attribute4 . $attribute5 . $attribute6 . $attribute7 . $attribute8 . $attribute9 . $attribute10; } $rank_min = 1; IF ($rank_max == ""){ $rank_max = 10; } Echo '<div class="getranks"> <table class="sortable" cellspacing="0;" align="center"> <thead> <tr> <th>First Name</th> <th>Last</th> <th>School</th> <th>Height</th> <th>Weight</th> <th>Commitment</th> <th>' . $rank_systems[0] . '</th> <th>' . $rank_systems[1] . '</th> <th>' . $rank_systems[2] . '</th> <th>' . $rank_systems[3] . '</th> </tr> </thead> <tbody>'; $i = 0; $result = mysql_query("SELECT $atstring, $rastring1 FROM players_heap as ph,players as p WHERE p.p_id =ph.p_id AND ph.graduation_year='$class' AND $ratring2 GROUP BY ph.p_id ORDER BY p.$rank1 ASC ")or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/136761-solved-help-with-complex-query-at-least-complex-to-me/ Share on other sites More sharing options...
peranha Posted December 12, 2008 Share Posted December 12, 2008 $ratring2 is that supposed to be $rastring2 Link to comment https://forums.phpfreaks.com/topic/136761-solved-help-with-complex-query-at-least-complex-to-me/#findComment-714266 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.