次の学生のレコードを含むテーブル名「Table1」があります。
student_id | Name | Math | English | Sience | Class
1 John 100 90 89 std two
2 Simon 100 100 100 std two
3 Irene 80 70 70 std two
学生パスマークごとのAVG、Totalを出力しようとしたのですが、全クラスの結果を出力できませんでした。
例:とりあえず検索するとこんな感じ。
student_id | Name | Math | English | Sience | Class Average Total
1 John 100 90 89 std two 93 279
別の学生を検索すると、希望の答えが得られますが、クラス名として「std two」を検索すると、すべてのクラスの代わりに1つのstudent_idを持つ最初の学生の結果が得られます。私が望む答えは、このようなものまたは任意の形式ですが、すべてのクラスの結果を出力する必要があります。
student_id | Name | Math | English | Sience | Class Average Total
1 John 100 90 89 std two 93 279
student_id | Name | Math | English | Sience | Class Average Total
2 Simon 100 100 100 std two 100 300
student_id | Name | Math | English | Sience | Class Average Total
3 Irene 80 70 70 std two 73.3 220
これは私のphpコードです
<?php
//include mysql connect
$query='query';
if (isset($_GET['query']))
{
$query=$_GET['query'];
}
$min_length = 2;
// you can set minimum length of the query if you want
if(strlen($query) >= $min_length){ // if query length is more or equal minimum
length
then
$query = htmlspecialchars($query);
// changes characters used in html to their equivalents, for example: < to >
$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection
$raw_results = mysql_query("SELECT *, AVG(math+english+science)/3 as
Average, (math+english+science)as Total from table1
WHERE (`name` LIKE '%".$query."%') or (`class` LIKE '%".$query."%')") or
die(mysql_error());
// '%$query%' is what we're looking for, % means anything, for example if $query
is Hello
// it will match "hello", "Hello man", "gogohello", if you want exact match use
`title`='$query'
// or if you want to match just full word so "gogohello" is out use '% $query %'
...OR ... '$query %' ... OR ... '% $query'
if(strlen($query) >= $min_length){
// if query length is more or equal minimum length the
if(mysql_num_rows($raw_results) >= 0){
// if one or more rows are returned do following
// $results = mysql_fetch_array($raw_results) puts data from database into
array, while it's valid it does the loop
while($results = mysql_fetch_array($raw_results)){
//The echo table
echo "<table width='500' height='2' cellpadding='2' cellspacing='0' border='0'>";
echo"<tr><td>Id_number</td><td>Name</td><td>Math</td><td>English</td><td>Science</td>
<td>Class</td><td>Average</td><td>Total</td>";
echo "<tr>"."<td>".$results["student_id"]."</td>"."<td>".$results["name"]."</td>"."
<td>".$results["math"]."</td>"."<td>".$results["english"]."</td>"."
<td>".$results["science"]."<td>".$results["class"]."<td>".$results["Average"]."</td>"."
<td>".$results["Total"]."</td>"."</p>";
echo"</table>";
}
}
}
}
?>