3
votes

I am currently making a WordPress plugin and trying to select a few values from WooCommerce database and all I keep getting is the same error. What might be wrong?

My Code:

function prntPage()
{
    //Connect is defined somewhere else
    $query = "SELECT * FROM `wp_woocommerce_order_items`";
    $result = mysqli_query($conn,$query);
     while($row = $result->mysqli_fetch_assoc()) {
        echo "id: " . $row["order_item_id"]. " - Product Name: " . $row["order_item_name"]. " - Order_ID" . $row["order_id"]. "<br>";
    }       
}

The Error:

Fatal error: Uncaught Error: Call to undefined method mysqli_result::mysqli_fetch_assoc() in C:\xampp\htdocs\ExamenProject\wp-content\plugins\drukkebaasjes-sizedata\drukkebaasjes-sizedata.php:54 Stack trace: #0 C:\xampp\htdocs\ExamenProject\wp-includes\class-wp-hook.php(286): prntPage('') #1 C:\xampp\htdocs\ExamenProject\wp-includes\class-wp-hook.php(310): WP_Hook->apply_filters('', Array) #2 C:\xampp\htdocs\ExamenProject\wp-includes\plugin.php(453): WP_Hook->do_action(Array) #3 C:\xampp\htdocs\ExamenProject\wp-admin\admin.php(224): do_action('toplevel_page_p...') #4 {main} thrown in C:\xampp\htdocs\ExamenProject\wp-content\plugins\drukkebaasjes-sizedata\drukkebaasjes-sizedata.php on line 54

1
$result->fetch_assoc() or mysqli_fetch_assoc($result). Every time you get an error like undefined method or undefined function you should check the manualsPaul Spiegel
Instead of using mysqli directly, you should look into using Wordpress db-class instead. You can read about it here: codex.wordpress.org/Class_Reference/wpdbMagnus Eriksson

1 Answers

2
votes

With Wordpress there is the WPDB class and methods for that purpose. Try the following instead:

function prntPage() {
    global $wpdb;

    // The SQL query
    $results = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}woocommerce_order_items");

    // Loop though rows data
    foreach( $results as $row ){
        echo "id: " . $row->order_item_id . " - Product Name: " . $row->order_item_name . " - Order_ID" . $row->order_id . "<br>";
    }
}

This function code works without error now.