1

I want to execute the following Query-String, which contains 2 separate queries using $wpdb->query:

CREATE TABLE `foobar` ( `id` int(2) NOT NULL AUTO_INCREMENT, `foo` varchar(22) COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=34 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;INSERT INTO `foobar` VALUES ('1','foo! mit so \' kram halt')

The query function returns just false and neither the table nor its row were created.

I helped me out by splitting the string into pieces using explode and execute each query separately:

$querString = "CREATE TABLE `foobar` ( `id` int(2) NOT NULL AUTO_INCREMENT, `foo` varchar(22) COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=34 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;INSERT INTO `foobar` VALUES ('1','foo! mit so \' kram halt')";

$queries = explode(';', $queryString);

foreach($queries as $query) {
  $wpdb->query($query);
}

That leads me to the question, why i can't i execute multiple-queries grouped in a string using $wbdb->query?

3
  • 1
    Almost always in these cases, a custom post type or taxonomy would be safer, faster to implement, and more efficient thanks to the internal caching system. If you really must create a custom database table, use dbdelta, and if you want to execute multiple queries, use separate strings or an array instead of explode. A custom post type and taxonomy will give you templates, archives, filtering, query vars, caching, APIs, a free admin UI, and allow you to use WP_Query to grab items, as well as plugin support, and in the future, REST API support
    – Tom J Nowell
    Aug 27, 2015 at 12:44
  • Hi @TomJNowell, thanks for your advice, which is very interesting, but my use case is a little bit different from what you might think. I want to store SQL-dumps from customers in the database to restore them later on. Because of the diversity of SQL, i wont try to port an SQL-dump to an array, only to store it now and use dbdelta afterwards. That might be an option later on, for sure!, but now, its only for a prototype. Do you have a guess, why multiple SQL statements can't be executed via $wpdb->query?
    – k00ni
    Aug 28, 2015 at 9:39
  • I will only say that WPDB was never intended for executing tens of thousands of queries all at once
    – Tom J Nowell
    Sep 3, 2015 at 2:51

1 Answer 1

1

Just separate query with ; as if you would be writting them in the normal sql editor. it should work. I assume your query are not reading query (returning results). they should be for CREATE, UPDATE, DELETE, ALTERsuch kind of queries not SELECT

2
  • Hi, i did separate them with ; but the query function returns just false and neither the table nor its row were created. I'd expected the function internally splits queries by ; and execute them sequentially. And yes, my queries or not of type SELECT.
    – k00ni
    Jan 6, 2017 at 15:41
  • Can you enable error viewing and see the error being thrown. you can refer to this answer as well. wordpress.stackexchange.com/questions/248998/…
    – Kamaro
    Jan 6, 2017 at 15:59

Your Answer

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

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