[英]Query that works in phpmyadmin is NOT working in php page?
我已經在phpmyadmin中測試了此查詢,它返回的正是我要查找的內容...它重復了row1並將標題更新為DUPLICATE 。
$sql = "CREATE TEMPORARY TABLE tmp
SELECT `unit_id`,
`title`,
`status_id`,
`category_id`,
`tags`,
`access_id`
FROM unit_genData
WHERE `unit_id`='1';
ALTER TABLE tmp
DROP COLUMN `unit_id`;
UPDATE tmp
SET `title` = 'DUPLICATE';
INSERT INTO unit_genData
SELECT 0,tmp.*
FROM tmp;
DROP TABLE tmp;";
然后,我將其添加到php頁面,然后...
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ALTER TABLE tmp DROP COLUMN
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ALTER TABLE tmp DROP COLUMN
unit_id You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ALTER TABLE tmp DROP COLUMN
; UPDATE tmp ' at line 10
; UPDATE tmp ' at line 10
為什么會出現此錯誤?
使用mysqli_multi_query()
執行多個查詢。
$sql = "CREATE TEMPORARY TABLE tmp
SELECT `unit_id`,
`title`,
`status_id`,
`category_id`,
`tags`,
`access_id`
FROM unit_genData
WHERE `unit_id`='1';
ALTER TABLE tmp
DROP COLUMN `unit_id`;
UPDATE tmp
SET `title` = 'DUPLICATE';
INSERT INTO unit_genData
SELECT 0,tmp.*
FROM tmp;
DROP TABLE tmp;";
$mysqli->multi_query($sql);
默認情況下,PHP在單個查詢中禁用多個語句。
可以分別運行兩個語句,也可以使用mysql_multi_query
。
http://php.net/manual/en/mysqli.quickstart.multiple-statement.php
(問題未指定使用哪個MySQL接口。)
請注意,如果您的代碼受SQL注入漏洞的影響,則為每個查詢啟用多個語句可能會為整個惡意行為打開大門。ala Little Bobby Tables http://xkcd.com/327/ 。
如果出於某種原因使用mysql
函數, CLIENT_MULTI_STATEMENTS
作為mysql_connect
的第五個參數傳遞,以允許查詢中包含多個語句。
更多信息在這里 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.