1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.