我有一个表名“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
我试图输出每个学生passmark的AVG,Total,但我没有输出所有班级的结果。
示例:现在我搜索时的结果是这样的。
student_id | Name | Math | English | Sience | Class Average Total
1 John 100 90 89 std two 93 279
当我搜索另一个学生时,我得到了想要的答案,但是当我搜索“std two”作为班级名称时,我得到第一个学生的结果,他有 1 个学生 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>";
}
}
}
}
?>