I tried to use the code above for truncate myTable.
As I open the page with a browser expecting all data in myTable is gone, but all records in myTable still in the table.
How should I use the code “truncate”?
Because TRUNCATE TABLE is equivalent in operation to DROP TABLE ... ; CREATE TABLE ...;, the account doing the truncating must have the rights to execute those operations in your database.
You can investigate what permissions the account has by issuing a SHOW GRANTS query.
If you dont see either “ALL” or “CREATE,DROP” in the response, your account does not have sufficient privilege to execute the command.
GRANT CREATE,DROP ON mydbname.* TO 'myusername'@'myhost', replacing mydbname, myusername, and myhost with the correct values.
(Note that the user issuing this command must themselves have privileges to grant privileges to said database.)
Perhaps that’s the problem, the OP is defining the variable with an equal-sign when they intended to use the (old) function with the string in brackets.