admin管理员组文章数量:1332352
Traditionally, whenever I've needed a real mysqli_fetch
for things like importing thousands of rows of data, I've used my own database connection and framework. But I was thinking there must be a way to use the existing database connection to do this, so I looked through the wp-includes/wp-db.php
file and determined that I can simply use the database handler that already exists and being used by the global $wpdb
database class instance.
In doing so, a query can be processed line by line, rather than attempting to load several thousand rows in to a the result from a $wpdb->get_results
call.
Here is my current solution:
$q = mysqli_query($wpdb->dbh,"SELECT * FROM some_table;");
while ($r = mysqli_fetch_assoc($q)) {
var_dump($r);
}
By simply using the protected
class property $wpdb->dbh
, it's allowing custom queries to be called without having to resort to adding a custom database framework to do heavy database processing work.
While this is working, I'm concerned that this may not be the best solution for being compatible with future versions of Wordpress as this is a bit of a hack solution, as this is not the normal expected use of $wpdb
. As such, I'm interested to know if anyone else found a better solution that is more based on the expected use of $wpdb
.
Note, I've looked at using $wpdb->get_row
along with the row number, but that appears to actually execute the query with each request anew, rather than using a single query and iterating through.
本文标签: Iterating through wpdb query without using getresults for large query results
版权声明:本文标题:Iterating through $wpdb query without using get_results for large query results 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742321639a2452907.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论