我有这个php查询,它连接了许多表(婴儿类到标准七),这样我就可以得到每个表的总SUM形式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 <?php echo $class;?>  class
in <?php echo $date;?> </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>
答案 0 :(得分:1)
在您的代码变量中,$q
没有在if条件之外访问的范围。
所以你需要在if条件之前定义它。在if条件上方添加以下行。
$q="";
代替while循环使用:
if(!empty($q)) {
while($res=mysql_fetch_array($q)){
-----------
your code
---------------
}
}
答案 1 :(得分:0)
尝试这样的事情
if($q === FALSE) {
die(mysql_error()); // TODO: better error handling
}
while($res=mysql_fetch_array($q))
{
//do here
}
答案 2 :(得分:0)
尝试:
while(!empty($q) and $res=mysql_fetch_array($q)){