1

I am executing a single query, then also why there is error 'Commands out of sync; you can't run this command now' in C:\wamp\www\classlearn\delete.php on line 65. Such error may cause during simultaneous queries, which is not in my case. Here's my code:

if(isset($_POST['delete'])){
    error_reporting(E_ALL);
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    $rn = $_POST['del_rn'];
    $check = $conn->prepare("SELECT name FROM `students` WHERE rollno=?");
    $check->bind_param("s",$rn);
    $check->execute();
    $rows = $check->fetch();
    if($rows>0){
        $stmt_student = $conn->prepare("DELETE FROM `students` WHERE rollno=?"); //line 65
        $stmt_student->bind_param("i",$rn);

        if($stmt_student->execute()){ 
            echo "Record deleted successfully";
        } else{ 
            echo "Record not deleted";
        }
    } else{ 
        echo "Record does not exists";
    }
}

I have checked query is correct and also tried with direct query instead of prepare statement. But, the effect is same. What could be the reason?

Thalow
  • 15
  • 5
  • 1
    Possible duplicate of [Commands out of sync; you can't run this command now](https://stackoverflow.com/questions/614671/commands-out-of-sync-you-cant-run-this-command-now) – Dharman Jul 08 '19 at 07:50
  • You try to run DELETE before SELECT is finished. – Dharman Jul 08 '19 at 07:51
  • @Dharman my question is not duplicate. Your link redirects to a question where the asker has simultaneous queries in code. But, this is not in my case. – Thalow Jul 08 '19 at 07:52
  • @Dharman so, what could be the solution? I want to achieve both things – Thalow Jul 08 '19 at 07:53
  • Change fetch to `get_results()->fetchAll()` and see if it helps. You try to execute 2 queries at the same time. SELECT and DELETE. – Dharman Jul 08 '19 at 07:55
  • @Dharman it worked by putting `$check->close()` – Thalow Jul 08 '19 at 07:57

0 Answers0