mysql - 'Commands out of sync; you can't run this command now' php sql error while executing multiple queries

944

I want to truncate multiple tables. Below is the code:

$del_all = $conn->prepare("TRUNCATE TABLE students"); //line 214
$res = $del_all->execute();
$del_all->store_result();
$del_all->close();

$del_all_fees = $conn->prepare("TRUNCATE TABLE fees");
$res_fees = $del_all_fees->execute();
$del_all_fees->store_result();
$del_all_fees->close();

$del_all_test = $conn->prepare("TRUNCATE TABLE test");
$res_test = $del_all_test->execute();
$del_all_test->store_result();
$del_all_test->close();

$del_all_jan = $conn->prepare("TRUNCATE TABLE jan");
$res_jan = $del_all_jan->execute();
$del_all_jan->store_result();
$del_all_jan->close();

$del_all_feb = $conn->prepare("TRUNCATE TABLE feb");
$res_feb = $del_all_feb->execute();
$del_all_feb->store_result();
$del_all_feb->close();

$del_all_mar = $conn->prepare("TRUNCATE TABLE mar");
$res_mar = $del_all_mar->execute();
$del_all_mar->store_result();
$del_all_mar->close();

if($res>0 && $res_fees>0 && $res_test>0 && $res_jan>0 && $res_feb>0 && $res_mar>0){
    echo "<div class='w3-panel w3-green w3-display-container' style='width:40%;'>
                <span onclick='this.parentElement.style.display='none''
                class='w3-button w3-large w3-display-topright'>&times;</span>
                <p>All Records deleted successfully</p>
            </div>";
} else{
    echo "<div class='w3-panel w3-green w3-display-container' style='width:40%;'>
                <span onclick='this.parentElement.style.display='none''
                class='w3-button w3-large w3-display-topright'>&times;</span>
                <p>Something went wrong. Records not deleted</p>
            </div>";
}

There is an error'Commands out of sync; you can't run this command now' in C:\wamp\www\classlearn\delete.php on line 214. I know the simultaneous queries cannot be executed, that's why I putclose() function between two queries and astore_result() after each query. But then also, it is giving the same error. What might be a problem? Please Help. I tried to code in a different way as below:

$del_all = "TRUNCATE TABLE students";

$del_all_fees = "TRUNCATE TABLE fees";

$del_all_test = "TRUNCATE TABLE test";                          

$del_all_jan = "TRUNCATE TABLE jan";                            

$del_all_feb = "TRUNCATE TABLE feb";                            

$del_all_mar = "TRUNCATE TABLE mar";

if($conn->query($del_all)&&$conn->query($del_all_fees)&&$conn->query($del_all_test)&&$conn->query($del_all_jan)&&$conn->query($del_all_feb)&&$conn->query($del_all_mar){
    echo "<p>All Records deleted successfully</p>"
} else
    echo "Failed to delete records";

Then also it didn't worked.

People are also looking for solutions to the problem: php - Make two files resize and save to different folders

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.