1

I want to truncate a table via PHP. It has some foreign keys, so I use the little trick where I set my foreign key check to zero:

$query_truncate_extension = "SET FOREIGN_KEY_CHECKS = 0; TRUNCATE TABLE extension; SET FOREIGN_KEY_CHECKS = 1;";

When I execute the script, the mysqli_error() gives me the following error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TRUNCATE TABLE extension; SET FOREIGN_KEY_CHECKS = 1' at line 1

So my table doesn't get truncated at all. But the weird thing is when I put this exact same query in the SQL-query section in phpmyadmin, it doesn't throw an error at all and my table is empty afterwards.

So my question is: why does this code gives an error in PHP, but not in phpmyadmin and how do I solve this?

Thanks in advance!

2
  • 3
    mysql commands in PHP don't support multiple queries in a single call, you have to call each of the queries in its own function call. Commented Oct 13, 2014 at 11:07
  • Can you share your PHP code and the exact error you're getting? Commented Oct 13, 2014 at 11:08

2 Answers 2

2

If you wan to run multiple queries in one call then you need to use

mysqli_multi_query()

This is a function allows to run one or multiple queries which are concatenated by a semicolon.

To retrieve the resultset from the first query you can use mysqli_use_result() or mysqli_store_result(). All subsequent query results can be processed using mysqli_more_results() and mysqli_next_result().

You can read more detail here.

http://php.net/manual/en/mysqli.multi-query.php

--------- Example Program at the link above -------------

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query  = "SELECT CURRENT_USER();";
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";

/* execute multi query */
if ($mysqli->multi_query($query)) {
    do {
        /* store first result set */
        if ($result = $mysqli->store_result()) {
            while ($row = $result->fetch_row()) {
                printf("%s\n", $row[0]);
            }
            $result->free();
        }
        /* print divider */
        if ($mysqli->more_results()) {
            printf("-----------------\n");
        }
    } while ($mysqli->next_result());
}

/* close connection */
$mysqli->close();

You can change it according to your requirement.

Sign up to request clarification or add additional context in comments.

1 Comment

I first tried the answer below (Rasels), so I had three seperate queries, but when I tried yours, the code executed way faster, so thank you both for a working answer :)
0

Use single query in single call like

$query_truncate_extension = "SET FOREIGN_KEY_CHECKS = 0"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.