持久警告:mysql_fetch_array()期望参数1是资源

时间:2013-02-21 11:46:43

标签: php

我一直试图找出导致此错误的原因,但出于某种原因。它只是不会出现!

它一直在给我这个警告信息:

  

警告:mysql_fetch_array()要求参数1为资源,第64行的C:\ xampp \ htdocs \ eis \ usercp.inc.php中给出布尔值

我尝试使用mysql_error();,但没有显示任何内容。

这是我的代码:

<?php
$firstname =  getuserfield('txtFname');
$lastname = getuserfield('txtLname');
echo 'Hello '.$firstname.' '.$lastname.'.<br/>';

$anotif = 0;
$dnotif = 0;
$anotif = $anotif + getuserfield('approved_notif');
$dnotif = $dnotif + getuserfield('disapproved_notif');
?>
<h3>Notifications:</h3>
Approved:<?php if($anotif<1){echo 0;} else { echo '<a href="?anotif">'.$anotif.'</a>';}?><br/>
<?php 
if(isset($_GET['anotif'])) {
    $query = "SELECT * FROM hrf_leave WHERE empid = '$empid' AND formStatus = 1 AND checked = 0";
    $query_run = mysql_query($query) or die($query."<br/><br/>".mysql_error());
    echo "<table border=1>
    <tr>
    <th>Type of Leave</th>
    <th>Specific Reason</th>
    <th>Date From</th>
    <th>Date To</th>
    <th>Number of Days</th>
    </tr>";
    echo "<tr>";
    while($record = mysql_fetch_array($query_run)){ // <------- this is line 64
        $leave_id = $record['leave_id'];
        echo "<td>" . $record['type_of_leave'] . "</td>";
        echo "<td>" . $record['specific_reason'] . "</td>";
        echo "<td>" . $record['date_from'] . "</td>";
        echo "<td>" . $record['date_to'] . "</td>";
        echo "<td>" . $record['num_of_days'] . "</td>";
        echo "</tr>";
        $query = "UPDATE hrf_leave SET checked=1 WHERE leave_id = $leave_id";
        if($query_run = mysql_query($query)){ 
            $anotif = $anotif-1;
            $query = "UPDATE hrf_leave SET approved_notif=$anotif WHERE empid = $empid";
            if($query_run = mysql_query($query)){ 
                echo 'Recently approved requests';
            }
        }
    }
    echo "</table>";
}
?>
Disapproved:<?php if($dnotif<1){echo 0;} else { echo '<a href="?dnotif">'.$dnotif.'</a><br/>';} ?>
<?php
if(isset($_GET['dnotif'])) {
    $query = "SELECT * FROM hrf_leave WHERE empid = '$empid' AND formStatus = 2 AND checked=0";
    $query_run = mysql_query($query);
    echo "<table border=1>
    <tr>
    <th>Type of Leave</th>
    <th>Specific Reason</th>
    <th>Date From</th>
    <th>Date To</th>
    <th>Number of Days</th>
    </tr>";
    echo "<tr>";
    while($record = mysql_fetch_array($query_run)){
        $leave_id = $record['leave_id'];
        echo "<td>" . $record['type_of_leave'] . "</td>";
        echo "<td>" . $record['specific_reason'] . "</td>";
        echo "<td>" . $record['date_from'] . "</td>";
        echo "<td>" . $record['date_to'] . "</td>";
        echo "<td>" . $record['num_of_days'] . "</td>";
        echo "</tr>";
        $query = "UPDATE hrf_leave SET checked=1 WHERE leave_id = $leave_id";
        if($query_run = mysql_query($query)){ 
            $dnotif = $dnotif-1;
            $query = "UPDATE hrf_leave SET disapproved_notif=$dnotif WHERE empid = $empid";
            if($query_run = mysql_query($query)){ 
                echo 'Recently disapproved requests';
            }
        }
    }
    echo "</table>";
}
?>

无论如何,我一直在尝试输出保留表格,其中检查字段值= 0.一旦看到,系统会将其设置为1.点击链接后,所有已批准和未批准的通知值将等于零

这是我的迷你通知系统。但由于这个错误,我无法让它工作..:/

2 个答案:

答案 0 :(得分:5)

在followind代码块中,产生警告的代码行使用的变量$query_run会被覆盖:

    $query = "UPDATE hrf_leave SET checked=1 WHERE leave_id = $leave_id";
    if($query_run = mysql_query($query)){ 
        $anotif = $anotif-1;
        $query = "UPDATE hrf_leave SET approved_notif=$anotif WHERE empid = $empid";
        if($query_run = mysql_query($query)){ 
            echo 'Recently approved requests';
        }
    }

使用其他变量名称,因为主$query_run调用

已使用mysql_fetch_array

答案 1 :(得分:0)

$query = "SELECT * FROM hrf_leave WHERE empid = '".$empid."' AND formStatus = 1 AND checked = 0";
echo $query;

然后在phpmyadmin中运行此查询,您可以看到错误纠正它。

您的查询容易受到SQL注入攻击。