最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to execute a SQL-query which contains multiple queries using $wpdb->query?

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Aug 28, 2015 at 9:43 k00ni asked Aug 27, 2015 at 11:14 k00nik00ni 1111 silver badge3 bronze badges 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 Commented 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 Commented 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 Commented Sep 3, 2015 at 2:51
Add a comment  | 

1 Answer 1

Reset to default 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

发布评论

评论列表(0)

  1. 暂无评论