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
?
1 Answer
Reset to default 1Just 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
, ALTER
such kind of queries not SELECT
dbdelta
, and if you want to execute multiple queries, use separate strings or an array instead ofexplode
. 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