简体   繁体   中英

Run multiple PHP MySQL queries

I have 9 MySQL queries to execute in PHP, and I am trying to execute them like this:

 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 = "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'

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.

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? 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 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. 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? That's why it's complaining that you named an unknown column 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; what I did was close the database and reopen between each call: $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.
As a matter of fact, almost every PHP script does such multiple query execution, this way or another.

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_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

of course, general PHP error reporting have to be turned on as well.

Without error message it is no use to guess.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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