How to execute a SQL-query which contains multiple queries using $wpdb->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
?
Leave an answer