选择查询以运行和显示结果

时间:2014-04-05 07:09:24

标签: php sql

我正在为学生信息创建教育结果页面。我基本上有2个查询,一个搜索从sql搜索滚动否,第二个是从名称搜索。当我将它组合在一个页面上时,我已经单独创建了它,它显示了两个结果,但我只需要一个结果,我的编码就是

<?php
include 'form.html';
$r1=$_GET["r"];
$n1=$_GET["n"];
$con=mysqli_connect(localhost,chumspai_tlss,Tls121,chumspai_tlsResult);

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$result = mysqli_query($con,"SELECT * FROM nursery_blue_ WHERE sr_='$r1'");

while($row = mysqli_fetch_array($result))


 {
        echo'<table width="100%" cellpadding="10" cellspacing="0" border="0">';
        echo'<tr>'; 
        echo'<td class="heading grey" width="30%">'.RNO.'</td>';
        echo'<td>'. $row['sr_']."</td>";
        echo'</tr>';
    echo'<tr>';
        echo'<td class="heading grey">'.NAME.'</td>';
    echo'<td class="shade">'. $row['students_names'].'</td>';
        echo'</tr>';
        echo'</table>';
        echo'<table width="100%" cellpadding="10" cellspacing="0" border="0">';
        echo'<tr>'; 
        echo'<tr class="grey">';
        echo'<td rowspan="2" class="heading">'.Sr_no.'</td>';
        echo'<td rowspan="2" class="heading">'.Subject.'</td>';
        echo'<td rowspan="2" class="heading">'.Total_Marks.'</td>';
        echo'<td rowspan="2" class="heading">'.Marks_Obtained.'</td>';
        echo'</tr>';
        echo'<tr>';
        echo'<td>'._.'</td>';
        echo'</tr>';
        echo'<tr>'; 
        echo'<td>'.(1).'</td>';
        echo'<td>'. English."</td>";
        echo'<td>'.(50).'</td>';
        echo'<td>'. $row['2']."</td>";
        echo'</tr>';
    echo'<tr>';
        echo'<td>'.(2).'</td>';
    echo'<td>'. Urdu.'</td>';
    echo'<td>'.(50).'</td>';
        echo'<td>'. $row['3']."</td>";
        echo'</tr>';
        echo'<tr>';
        echo'<td>'.(3).'</td>';
    echo'<td>'. Math.'</td>';
    echo'<td>'.(50).'</td>';
        echo'<td>'. $row['4']."</td>";
        echo'</tr>';
        echo'<tr>';
        echo'<td>'.(4).'</td>';
    echo'<td>'. G_know.'</td>';
    echo'<td>'.(50).'</td>';
        echo'<td>'. $row['5']."</td>";
        echo'</tr>';
        echo'<tr>';
        echo'<td>'.(5).'</td>';
    echo'<td>'. Act_Con.'</td>';
    echo'<td>'.(30).'</td>';
        echo'<td>'. $row['6']."</td>";
        echo'</tr>';
        echo'<tr>';
        echo'<td>'.(6).'</td>';
    echo'<td>'. Islamiat.'</td>';
    echo'<td>'.(20).'</td>';
        echo'<td>'. $row['7']."</td>";
        echo'</tr>';
        echo'<tr>';
        echo'<td>'.(7).'</td>';
    echo'<td>'. Quran.'</td>';
    echo'<td>'.(25).'</td>';
        echo'<td>'. $row['8']."</td>";
        echo'</tr>';
    echo'<tr>';
    echo'<td colspan="2" class="heading grey">'.TOTAL.'</td>';
    echo'<td colspan="1" class="heading">'.$row['10'].'</td>';
    echo'<td colspan="1" class="heading">'.$row['9'].'</td>';
        echo'</tr>';
        echo'<tr>';
        echo'<td colspan="3" class="heading grey">'.Persantage.'</td>';
        echo'<td colspan="1" class="heading">'.$row['11'].'</td>';
        echo'</tr>';
        echo'<tr>';
        echo'<td colspan="3" class="heading grey">'.Grade.'</td>';
        echo'<td colspan="1" class="heading">'.$row['12'].'</td>';
        echo'</tr>';
        echo'<tr>';
        echo'<td colspan="3" class="heading grey">'.Lecture_Delivered.'</td>';
        echo'<td colspan="1" class="heading">'.$row['13'].'</td>';
        echo'</tr>';
        echo'<tr>';
        echo'<td colspan="3" class="heading grey">'.Attendence.'</td>';
        echo'<td colspan="1" class="heading">'.$row['14'].'</td>';
        echo'</tr>';
    echo'<tr>';
        echo'<td colspan="7" class="heading grey">'."(i) This provisional result intimation is issued as a notice only. Errors and omissions are excepted.".'</td>';
        echo'</tr>';

        echo'</table>';
    }
$result1 = mysqli_query($con,"SELECT * FROM nursery_blue_ WHERE students_names like '%$n1%'");

while($row1 = mysqli_fetch_array($result1))
{
echo '<pre>';
     echo "<a href='http://www.tlss.edu.pk/result/index.php?r=".$row1['sr_']."'>".$row1['sr_'].'.'.$row1['students_names']."</a>";
  echo '</pre>';
  }    


?>

请帮忙!

1 个答案:

答案 0 :(得分:1)

如果要在页面上显示一个结果集,则一次传递一个查询字符串parm。 像这样http://yoursite.com/result.php?n=johnhttp://yoursite.com/result.php?r=5566 并将您的代码更改为此。

(代码未经过测试)

<?php
include 'form.html';
$con=mysqli_connect(localhost,username,password,database);

if (mysqli_connect_errno())
  {
  exit("Failed to connect to MySQL: " . mysqli_connect_error());
  }

if(isset($_GET['r'])) {
    // You must filter the User input .. to stop SQL injection attack's
 $r1 =  mysql_real_escape_string($_GET['r']);
$query = "SELECT * FROM nursery_blue_ WHERE sr_='$r1'";
$result = mysqli_query($con , $query);
// Proccess Loop
while($row = mysqli_fetch_array($result))

 {
        echo'<table width="100%" cellpadding="10" cellspacing="0" border="0">';
        echo'<tr>'; 
        echo'<td class="heading grey" width="30%">'.RNO.'</td>';
        echo'<td>'. $row['sr_']."</td>";
        echo'</tr>';
    echo'<tr>';
        echo'<td class="heading grey">'.NAME.'</td>';
    echo'<td class="shade">'. $row['students_names'].'</td>';
        echo'</tr>';
        echo'</table>';
        echo'<table width="100%" cellpadding="10" cellspacing="0" border="0">';
        echo'<tr>'; 
        echo'<tr class="grey">';
        echo'<td rowspan="2" class="heading">'.Sr_no.'</td>';
        echo'<td rowspan="2" class="heading">'.Subject.'</td>';
        echo'<td rowspan="2" class="heading">'.Total_Marks.'</td>';
        echo'<td rowspan="2" class="heading">'.Marks_Obtained.'</td>';
        echo'</tr>';
        echo'<tr>';
        echo'<td>'._.'</td>';
        echo'</tr>';
        echo'<tr>'; 
        echo'<td>'.(1).'</td>';
        echo'<td>'. English."</td>";
        echo'<td>'.(50).'</td>';
        echo'<td>'. $row['2']."</td>";
        echo'</tr>';
    echo'<tr>';
        echo'<td>'.(2).'</td>';
    echo'<td>'. Urdu.'</td>';
    echo'<td>'.(50).'</td>';
        echo'<td>'. $row['3']."</td>";
        echo'</tr>';
        echo'<tr>';
        echo'<td>'.(3).'</td>';
    echo'<td>'. Math.'</td>';
    echo'<td>'.(50).'</td>';
        echo'<td>'. $row['4']."</td>";
        echo'</tr>';
        echo'<tr>';
        echo'<td>'.(4).'</td>';
    echo'<td>'. G_know.'</td>';
    echo'<td>'.(50).'</td>';
        echo'<td>'. $row['5']."</td>";
        echo'</tr>';
        echo'<tr>';
        echo'<td>'.(5).'</td>';
    echo'<td>'. Act_Con.'</td>';
    echo'<td>'.(30).'</td>';
        echo'<td>'. $row['6']."</td>";
        echo'</tr>';
        echo'<tr>';
        echo'<td>'.(6).'</td>';
    echo'<td>'. Islamiat.'</td>';
    echo'<td>'.(20).'</td>';
        echo'<td>'. $row['7']."</td>";
        echo'</tr>';
        echo'<tr>';
        echo'<td>'.(7).'</td>';
    echo'<td>'. Quran.'</td>';
    echo'<td>'.(25).'</td>';
        echo'<td>'. $row['8']."</td>";
        echo'</tr>';
    echo'<tr>';
    echo'<td colspan="2" class="heading grey">'.TOTAL.'</td>';
    echo'<td colspan="1" class="heading">'.$row['10'].'</td>';
    echo'<td colspan="1" class="heading">'.$row['9'].'</td>';
        echo'</tr>';
        echo'<tr>';
        echo'<td colspan="3" class="heading grey">'.Persantage.'</td>';
        echo'<td colspan="1" class="heading">'.$row['11'].'</td>';
        echo'</tr>';
        echo'<tr>';
        echo'<td colspan="3" class="heading grey">'.Grade.'</td>';
        echo'<td colspan="1" class="heading">'.$row['12'].'</td>';
        echo'</tr>';
        echo'<tr>';
        echo'<td colspan="3" class="heading grey">'.Lecture_Delivered.'</td>';
        echo'<td colspan="1" class="heading">'.$row['13'].'</td>';
        echo'</tr>';
        echo'<tr>';
        echo'<td colspan="3" class="heading grey">'.Attendence.'</td>';
        echo'<td colspan="1" class="heading">'.$row['14'].'</td>';
        echo'</tr>';
    echo'<tr>';
        echo'<td colspan="7" class="heading grey">'."(i) This provisional result intimation is issued as a notice only. Errors and omissions are excepted.".'</td>';
        echo'</tr>';

        echo'</table>';
    }




} else if(isset($_GET['n'])){
 $n1 =  mysql_real_escape_string($_GET['n']);
$query = "SELECT * FROM nursery_blue_ WHERE students_names like '%$n1%'";
$result = mysqli_query($con , $query);


while($row1 = mysqli_fetch_array($result1))
{
echo '<pre>';
     echo "<a href='http://www.tlss.edu.pk/result/index.php?r=".$row1['sr_']."'>".$row1['sr_'].'.'.$row1['students_names']."</a>";
  echo '</pre>';
  } 


} else {
    exit();
}

?>