繁体   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