Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run multiple PHP MySQL queries

Tags:

php

mysql

mysqli

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)";
like image 982
Wingdom Avatar asked Sep 20 '26 12:09

Wingdom


1 Answers

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);
like image 82
Bill Karwin Avatar answered Sep 23 '26 01:09

Bill Karwin