如何将此警告删除到此php脚本


How to remove this warning to this php script

我有这个php查询,它连接了来自(baby类到标准七)的许多表,这样我就可以从每个表的SUM中获得Total SUM。

但当我运行查询时,它会给我警告说

Warning: mysql_fetch_array() expects   
parameter 1 to be resource, boolean given in   
C:'wamp'www'db_valentine_V2'Templates'school_income.php on line 872

我试图在第872行中找出这个脚本的位置

while($res=mysql_fetch_array($q)){

有人能帮我消除这个警告吗?它真的花了我很长时间,但没有成功。

  <?php
 $date="date";
 $class="class";
 $database=("mcl");
 mysql_connect("localhost","root","mcl");
 @mysql_select_db(mcl) or die( "Unable to select database");

if(isset($_REQUEST['submit'])){
$class=$_POST['class'];
$date=$_POST['date'];

$sql="SELECT SUM(Total) FROM 
    ( SELECT     
   SUM(school_fee+trans_fee+reg_fee+edutrip_fee+stationery_fee+food_fee+
  uniform_fee+sport_uniform) As Total FROM payment UNION  
    SELECT SUM(school_fee+trans_fee+reg_fee+edutrip_fee+stationery_fee+
   food_fee+uniform_fee+sport_uniform) As Total FROM payment_one UNION
  SELECT    SUM(school_fee+trans_fee+reg_fee+edutrip_fee+stationery_fee+food_fee
  +uniform_fee+sport_uniform) As Total   
        FROM payment_two UNION
   SELECT   SUM(school_fee+trans_fee+reg_fee+edutrip_fee+stationery_fee+food_fee
  +uniform_fee+sport_uniform) As Total   
        FROM payment_three UNION
  SELECT   
 SUM(school_fee+trans_fee+reg_fee+edutrip_fee+stationery_fee+food_fee+
 uniform_fee+sport_uniform) As Total   
        FROM payment_four UNION
  SELECT SUM(school_fee+trans_fee+reg_fee+edutrip_fee+stationery_fee+food_fee+
  uniform_fee+sport_uniform) As Total   
        FROM payment_five UNION
 SELECT   SUM(school_fee+trans_fee+reg_fee+edutrip_fee+stationery_fee+food_fee+
 uniform_fee+sport_uniform) As Total   
        FROM payment_six UNION
 SELECT SUM(school_fee+trans_fee+reg_fee+edutrip_fee+stationery_fee+food_fee+
 uniform_fee+sport_uniform) As Total   
        FROM payment_seven )a)";

 $q=mysql_query($sql);  
    }
  ?>

<form method="post">
<table width="500" border="0">
 <tr>
<td>Class</td>
<td><input type="text" name="class" value="<?php echo $class;?>" /></td>
<td>Year</td>
<td><input type="number" name="date" value="<?php echo $date;?>" /></td>
<td><input type="submit" name="submit" value="Search" /></td>
 </tr>
</table>
<br />
   </form>
    <table>
    <h4>
   This is total amount of money payed by&nbsp;<?php echo $class;?> &nbspclass     
   in&nbsp<?php echo $date;?>&nbsp;</h4><hr> 
   <table width="700" border="0" height="2" cellpadding="0" cellspacing="1" >
   <tr>
    <td bgcolor="#FF9900" width="50">Class</td>
     <td bgcolor="#FF9900" width="300">Total amount of money payed</td>
   </tr>
  <?php
   while($res=mysql_fetch_array($q)){
       ?>
     <tr>
     <td width="100"><?php echo $res['class'];?></td>
     <td width="200"><?php echo $res['SUM(Total)'];?></td>
     </tr>
    <?php }?>    

     </table>          

在代码中,变量$q在if条件之外没有访问的范围。

所以你需要在if条件之前定义它。在if条件上方添加以下行。

$q="";

代替while循环使用以下内容:

if(!empty($q)) {
   while($res=mysql_fetch_array($q)){
           -----------
          your code
          ---------------
   }
}

尝试:

while(!empty($q) and $res=mysql_fetch_array($q)){

尝试类似的东西

if($q === FALSE) {
        die(mysql_error()); // TODO: better error handling
    }
while($res=mysql_fetch_array($q))
{
    //do here
}