期望参数2是资源

时间:2017-04-29 10:58:08

标签: php mysql

我想解决这两个错误:

  

mysql_query()期望参数2是资源,第46行的C:\ wamp \ www \ trial \ search.php中给出的对象

     

mysql_fetch_array()期望参数1为资源,在第67行的C:\ wamp \ www \ trial \ search.php中给出null

<html>
    <head>
    <title>
    </title>
    </head>
    <body>
    <form method="post" action="">
    <div align="center">
    <table>
    <tr>
    <td>


    <select name="month">
     <option value=""></option>                
    <option value="january">jan</option>
    <option value="febuary">feb</option>
    <option value="march">mar</option>
    <option value="april">apr</option>
    <option value="may">mar</option>
    <option value="june">jun</option>
    <option value="july">jul</option>
    <option value="august">aug</option>
    <option value="september">sep</option>
    <option value="october">oct</option>
    <option value="november">nov</option>
    <option value="december">dec</option>

    </select>
    </td>
    <td>
    <input type="submit" name="submit" value="submit">
    </td>
    </tr>
    </table>
    </div>
    <div align="center">

    <?php
    include 'config.php';


    if(isset($_POST['month']))
    {
     echo $month=$_POST['month'];
    $query=mysql_query("select empname,tasktype,month,W1,W2,W3,W4,W5 from task_update where month ='$month'",$conn);
    //$getmonth=mysql_query("select * from empname where empname='arjun'",$conn);


    }

    ?>
    <table>
    <thead>
             <th>sno</th>
             <th>empname</th>
             <th>tasktype</th>
             <th>month</th>
             <th>W1</th>
             <th>W2</th>
             <th>W3</th>
             <th>W4</th>
             <th>W5</th>
    </thead>
    <?php
    $i=1;
    while($disp=mysql_fetch_array($getmonth))
    {
        ?>
        <tr>
        <td>$i</td>
        <td>$disp['empname']</td>
        <td>$disp['tasktype']</td>
        <td>$disp['month']</td>
        <td>$disp['W1']</td>
        <td>$disp['W2']</td>
        <td>$disp['W3']</td>
        <td>$disp['W4']</td>
        <td>$disp['W5']</td>

        </tr>
        <?php
    $i++;}

    ?>

    </table>
    </form>
    </body>
</html>

3 个答案:

答案 0 :(得分:0)

你传递$ getmonth需要传递$ query,最好的做法是在传入php mysql函数之前检查变量。

 if (!$query) {
    while($disp=mysql_fetch_array($query))
    {
    //your code
    }
    }

请使用Mysqli或Pdo Mysql不再有效

答案 1 :(得分:0)

你可以var_dump $ conn来检查它是否是一个有效的mysql资源

  

的var_dump($ conn);在

答案 2 :(得分:0)

  

警告mysql_query,mysql_fetch_array,mysql_connect等..扩展在PHP 5.5.0中已弃用,并且已在PHP 7.0.0中删除。   相反,应该使用MySQLi或PDO_MySQL扩展。

         <?php

         //db connection

        global $conn;

        $servername = "localhost";  //host name

        $username = "username"; //username

        $password = "password"; //password

        $mysql_database = "dbname"; //database name

    //mysqli prepared statement 

        $conn = mysqli_connect($servername, $username, $password) or die("Connection failed: " . mysqli_connect_error());

       mysqli_select_db($conn,$mysql_database) or die("Opps some thing went wrong");


        if(isset($_POST['month']))
        {
          $month=$_POST['month'];

        $stmt = $conn->prepare("select empname,tasktype,month,W1,W2,W3,W4,W5 from task_update where month =?");

        $stmt->bind_param('s',$month);

        //The argument may be one of four types:

        //i - integer
        //d - double
        //s - string
        //b - BLOB
        //change it by respectively 


        $stmt->execute();
        $get_result =$stmt->get_result();

        $row_count= $get_result->num_rows;


        }

        ?>
        <table>
        <thead>
                 <th>sno</th>
                 <th>empname</th>
                 <th>tasktype</th>
                 <th>month</th>
                 <th>W1</th>
                 <th>W2</th>
                 <th>W3</th>
                 <th>W4</th>
                 <th>W5</th>
        </thead>
        <tbody>
        <?php

        if(isset($row_count) && $row_count>0){
            $i=1;
            while($disp=$get_result->fetch_assoc())
            {
                ?>
                <tr>
                <td><?php echo $i; ?></td>
                <td><?php echo $disp['empname']; ?></td>
                <td><?php echo $disp['tasktype']; ?></td>
                <td><?php echo $disp['month']; ?></td>
                <td><?php echo $disp['W1']; ?></td>
                <td><?php echo $disp['W2']; ?></td>
                <td><?php echo $disp['W3']; ?></td>
                <td><?php echo $disp['W4']; ?></td>
                <td><?php echo $disp['W5']; ?></td>

                </tr>
                <?php
            $i++;
            }
        }
        ?>
        </tbody>
        </table>