[英]MySQL in PHP gives error, MySQL in phpmyadmin does not
我想通過PHP截斷表。 它具有一些外鍵,因此我使用了一個小技巧,將外鍵檢查設置為零:
$query_truncate_extension = "SET FOREIGN_KEY_CHECKS = 0; TRUNCATE TABLE extension; SET FOREIGN_KEY_CHECKS = 1;";
當我執行腳本時,mysqli_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
所以我的桌子根本不會被截斷。 但是很奇怪的是,當我在phpmyadmin的SQL-query部分中放置這個完全相同的查詢時,它根本不會引發任何錯誤,並且之后我的表為空。
所以我的問題是:為什么這段代碼在PHP中會給出錯誤,而在phpmyadmin中卻沒有,我該如何解決呢?
提前致謝!
如果您想在一個呼叫中運行多個查詢,則需要使用
mysqli_multi_query()
此功能允許運行由分號連接的一個或多個查詢。
要從第一個查詢中檢索結果集,可以使用mysqli_use_result()或mysqli_store_result()。 可以使用mysqli_more_results()和mysqli_next_result()處理所有后續查詢結果。
您可以在此處閱讀更多詳細信息。
http://php.net/manual/en/mysqli.multi-query.php
---------上方鏈接中的示例程序-------------
$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();
您可以根據需要進行更改。
在單個通話中使用單個查詢,例如
$query_truncate_extension = "SET FOREIGN_KEY_CHECKS = 0"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.