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

Iterating through $wpdb query without using get_results for large query results

programmeradmin2浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论