-1

I want to solve these two errors:

mysql_query() expects parameter 2 to be resource, object given in C:\wamp\www\trial\search.php on line 46

mysql_fetch_array() expects parameter 1 to be resource, null given in C:\wamp\www\trial\search.php on line 67

<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>
Community
  • 1
  • 1
arjun
  • 55
  • 6

3 Answers3

0

You are passing $getmonth you need to pass $query, best practice is check variable before pass in php mysql functions.

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

kindly use Mysqli or Pdo Mysql is no longer valid

Ahmed Ginani
  • 6,396
  • 2
  • 13
  • 32
0

You could var_dump the $conn to check if it is a valid mysql resource

var_dump($conn);

Tosin
  • 116
  • 1
  • 3
0

Warning mysql_query, mysql_fetch_array,mysql_connect etc.. extensions were deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.

         <?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>
JYoThI
  • 11,793
  • 1
  • 10
  • 25