简体   繁体   English

运行多个PHP MySQL查询

[英]Run multiple PHP MySQL queries

I have 9 MySQL queries to execute in PHP, and I am trying to execute them like this: 我有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><?
 }

But for some reason, only the first one is being executed, and showing up in the DB. 但由于某种原因,只有第一个正在执行,并在数据库中显示。 The rest are not completing. 其余的都没有完成。 Is there something I am missing about executing multiple queries, or is there a syntax mistake I am not seeing? 有没有关于执行多个查询的遗漏,或者是否存在我没​​有看到的语法错误?

And here is $q2, because it doesn't seem to want to get past that: 这里是$ 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)";

Unknown column 'test' in 'field list' “字段列表”中的未知列'test'

This can happen if you fail to delimit string literals in single-quotes. 如果您无法在单引号中分隔字符串文字,则会发生这种情况。

Take these two statements for example: 以这两个语句为例:

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

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

The difference is that the test in query 1 is assumed to be a column identifier, whereas the test in query 2 is a string literal. 不同之处在于,查询1中的test被假定为列标识符,而查询2中的test是字符串文字。

I know it seems to make no sense to use a column identifier in the VALUES clause for a new row -- how could that column have any value, if the row hasn't been inserted yet? 我知道在VALUES子句中为新行使用列标识符似乎毫无意义 - 如果该行尚未插入,该列如何具有任何值? In fact if you were to name columns that exist in this table, the INSERT works, but the column values are NULL for a new row. 实际上,如果要命名此表中存在的列,则INSERT可以正常工作,但新行的列值为NULL。

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

In your example query you have: 在您的示例查询中,您有:

$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)";

To help debug, you could echo $q2 and see what the SQL really looks like before you execute it. 为了帮助调试,您可以echo $q2并在执行之前查看SQL的真实情况。 I expect it'll be something like this: 我希望它会是这样的:

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)

See the test without quotes in that query? 查看该查询中没有引号的test That's why it's complaining that you named an unknown column test . 这就是为什么它抱怨你命名了一个未知的列test

tip: it's better to use prepared statements when you want to pass application variables to a query, for the reason that you don't have to worry about quotes around the parameters: 提示:当您想要将应用程序变量传递给查询时,最好使用预准备语句,因为您不必担心参数周围的引号:

$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);

NEWBIE with PHP...I had the same issue, trying to run consecutive queries, simple ones, just basic select and update; 使用PHP的NEWBIE ...我遇到了同样的问题,尝试运行连续查询,简单查询,只需基本选择和更新; what I did was close the database and reopen between each call: $dbh->close(); 我做的是关闭数据库并在每次调用之间重新打开:$ dbh-> close();

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

Is there something I am missing about executing multiple queries? 有没有关于执行多个查询的遗漏?

NO. 没有。

there is apparently nothing special in running multiple queries in mysqli in general. 一般来说,在mysqli中运行多个查询显然没什么特别之处。
As a matter of fact, almost every PHP script does such multiple query execution, this way or another. 事实上,几乎每个 PHP脚本都以这种或那种方式执行这样的多个查询。

is there a syntax mistake I am not seeing? 我没有看到语法错误吗?

Ask your computer , not humans. 问你的电脑 ,而不是人类。 A program intended to be run by computer - so, the only way to tell if syntax is wrong or there is another issue is to run a program. 一个旨在由计算机运行的程序 - 因此,判断语法是否错误或存在另一个问题的唯一方法是运行程序。

only the first one is being executed 只有第一个正在执行

If some query didn't run, there was an error. 如果某些查询未运行,则会出现错误。
Mysqli doesn't report mysql errors by default, you have to set it explicitly: Mysqli默认不报告mysql错误,你必须明确设置它:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

of course, general PHP error reporting have to be turned on as well. 当然,一般的PHP错误报告也必须打开。

Without error message it is no use to guess. 没有错误消息,猜测是没有用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM