how to select data from a database using SQL PDO

  pdo, php

Am trying to select data from the database using a function and SQL PDO but am getting this error

Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in

please help me out somebody

public function fetchAllPastQuestion($level,$programs_code)
{
 $sql = "SELECT *  FROM questions_bsc_actu_sci   WHERE deleted IS NULL AND level = '$level' AND program = $programs_code";

  $stmt = $this->conn->prepare($sql);
 $stmt->execute(['level'=>$level,'program'=>$programs_code]);
 $result = $stmt->fetchAll(PDO::FETCH_ASSOC);

  return $result;
}


//calling function
if(isset($_POST['action']) && $_POST['action'] == 'fetchAllPastQuestion' ){
    $output='';
    $data = $cuser->fetchAllPastQuestion($clevel,$cprograms_code);
    $path = '../assets/php/';
    if($data){
        $output.= '<table class="table table-striped table-bordered text-center  ">
        <thead>
            <tr>
         <th class="">Action</th>
        
         <th class="">File Name</th>
          <th class="">File Type</th>
         <th class="">Date Uploaded</th>
            </tr>
        </thead>
        <tbody>';
        foreach ($data as $row){
        
            $output .='<tr >
         <td class=" "><a class="btn font-weight-bold text-success" href="user-admin/download_questions.php?id=' .$row['id']. 'class="btn font-weight-bold text-success"><span class="glyphicon glyphicon-download"></span> Download</a>
            
            <td>'.substr($row['filename'],0 , 30).  '</td>
         <td>'.$row['file_type'].'</td>
            <td>'.$cuser->timeInAgo($row['uploaded_date']).'</td>
   
        
        </tr>';
        }
            $output.= '</tbody>
                    </table>';
                    
                    echo $output;


    }
    else{
        echo '<h3 class="text-center text-muted">:(No any question is posted yet </h3>';
    }
}

Source: Ask PHP

LEAVE A COMMENT