簡體   English   中英

運行多個PHP MySQL查詢

[英]Run multiple PHP MySQL queries

我有9個MySQL查詢要在PHP中執行,我試圖像這樣執行它們:

 if(mysqli_query($con, $q1)){
    ?><p>Query 1 complete</p><?
 }
 if(mysqli_query($con, $q2)){
    ?><p>Query 2 complete</p><?
 }
 if(mysqli_query($con, $q3)){
    ?><p>Query 3 complete</p><?
 }
 if(mysqli_query($con, $q4)){
    ?><p>Query 4 complete</p><?
 }
 if(mysqli_query($con, $q5)){
    ?><p>Query 5 complete</p><?
 }
 if(mysqli_query($con, $q6)){
    ?><p>Query 6 complete</p><?
 }
 if(mysqli_query($con, $q7)){
    ?><p>Query 7 complete</p><?
 }
 if(mysqli_query($con, $q8)){
    ?><p>Query 8 complete</p><?
 }
 if(mysqli_query($con, $q9)){
    ?><p>Query 9 complete</p><?
 }

但由於某種原因,只有第一個正在執行,並在數據庫中顯示。 其余的都沒有完成。 有沒有關於執行多個查詢的遺漏,或者是否存在我沒​​有看到的語法錯誤?

這里是$ q2,因為它似乎不想過去:

$q2 = "INSERT INTO outboundapps 
        (appid, 
         fk_outboundappkey, 
         name, 
         browser, 
         fk_urls, 
         fk_routes, 
         virtualplatform, 
         autoanswer, 
         fk_get) 
VALUES   ($last + 2, 
         $last + 2, 
         $oname, 
         $otype, 
         $last + 2, 
         $last + 2, 
         $ovirtualplatform, 
         1, 
         $last + 2)";

“字段列表”中的未知列'test'

如果您無法在單引號中分隔字符串文字,則會發生這種情況。

以這兩個語句為例:

1. INSERT INTO mytable (col1) VALUES (test);

2. INSERT INTO mytable (col1) VALUES ('test');

不同之處在於,查詢1中的test被假定為列標識符,而查詢2中的test是字符串文字。

我知道在VALUES子句中為新行使用列標識符似乎毫無意義 - 如果該行尚未插入,該列如何具有任何值? 實際上,如果要命名此表中存在的列,則INSERT可以正常工作,但新行的列值為NULL。

INSERT INTO mytable (col1) VALUES (col1); -- no error, but inserts only a NULL

在您的示例查詢中,您有:

$q2 = "INSERT INTO outboundapps 
        (appid, 
         fk_outboundappkey, 
         name, 
         browser, 
         fk_urls, 
         fk_routes, 
         virtualplatform, 
         autoanswer, 
         fk_get) 
VALUES   ($last + 2, 
         $last + 2, 
         $oname, 
         $otype, 
         $last + 2, 
         $last + 2, 
         $ovirtualplatform, 
         1, 
         $last + 2)";

為了幫助調試,您可以echo $q2並在執行之前查看SQL的真實情況。 我希望它會是這樣的:

INSERT INTO outboundapps 
        (appid, 
         fk_outboundappkey, 
         name, 
         browser, 
         fk_urls, 
         fk_routes, 
         virtualplatform, 
         autoanswer, 
         fk_get) 
VALUES   (125, 
         125, 
         test, 
         Firefox, 
         125, 
         125, 
         test, 
         1, 
         125)

查看該查詢中沒有引號的test 這就是為什么它抱怨你命名了一個未知的列test

提示:當您想要將應用程序變量傳遞給查詢時,最好使用預准備語句,因為您不必擔心參數周圍的引號:

$q2 = "INSERT INTO outboundapps 
        (appid, 
         fk_outboundappkey, 
         name, 
         browser, 
         fk_urls, 
         fk_routes, 
         virtualplatform, 
         autoanswer, 
         fk_get) 
VALUES   (?, ?, ?, ?, ?, ?, ?, 1, ?)";

$stmt = mysqli_prepare($q2) or trigger_error(mysqli_error(), E_USER_ERROR);
$stmt->bind_param("iissiisi", $last2, $last2, $oname, $otype, $last2, 
    $last2, $ovirtualplatform, $last2);
$stmt->execute() or trigger_error($stmt->error, E_USER_ERROR);

使用PHP的NEWBIE ...我遇到了同樣的問題,嘗試運行連續查詢,簡單查詢,只需基本選擇和更新; 我做的是關閉數據庫並在每次調用之間重新打開:$ dbh-> close();

$dbh = db::GetInstance(); this was in a tutorial for SINGLETON db, seems to work ok

有沒有關於執行多個查詢的遺漏?

沒有。

一般來說,在mysqli中運行多個查詢顯然沒什么特別之處。
事實上,幾乎每個 PHP腳本都以這種或那種方式執行這樣的多個查詢。

我沒有看到語法錯誤嗎?

問你的電腦 ,而不是人類。 一個旨在由計算機運行的程序 - 因此,判斷語法是否錯誤或存在另一個問題的唯一方法是運行程序。

只有第一個正在執行

如果某些查詢未運行,則會出現錯誤。
Mysqli默認不報告mysql錯誤,你必須明確設置它:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

當然,一般的PHP錯誤報告也必須打開。

沒有錯誤消息,猜測是沒有用的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM