1
// php, using wordpress to send a raw query string
    $output = $wpdb->query("
      INSERT INTO wp_priceGrids (gridId) VALUES (NULL); SELECT MAX(gridId) FROM wp_priceGrids;
    ");

Results in:

WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT MAX(gridId) FROM wp_priceGrids' at line 1]
INSERT INTO wp_priceGrids (gridId) VALUES (NULL); SELECT MAX(gridId) FROM wp_priceGrids;

Tried

  1. Deleting / commenting the 2nd line. Works. A new row is added and visible as I check through CLI
  2. Adding backticks (``) around the field name gridId. Seems not to affect anything, error is the same
  3. Removing the hard return between statements (as you can see above). No change

Question

Is it a syntax error? I've checked against examples and I think what I have matches their formats

owner@G700:~$ mysql --version
mysql  Ver 14.14 Distrib 5.7.25, for Linux (x86_64) using  EditLine wrapper
owner@G700:~$ php --version
PHP 7.2.15-0ubuntu0.18.04.1 (cli) (built: Feb  8 2019 14:54:22) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Xdebug v2.7.0, Copyright (c) 2002-2019, by Derick Rethans
    with Zend OPcache v7.2.15-0ubuntu0.18.04.1, Copyright (c) 1999-2018, by Zend Technologies
0

1 Answer 1

1

Your multiquery is subject to a race condition. You may not get back the value of the insert you do.

Use insert-id like:

$output = $wpdb->query("
      INSERT INTO wp_priceGrids (gridId) VALUES (NULL)";
$lastid = $wpdb->insert_id;

I've also heard the multiquery aspect of the mysql protocol is generally considered a mistake. Recommend avoiding it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.