尝试从数据库

时间:2018-05-28 20:29:29

标签: php mysql sql wordpress woocommerce

我目前正在制作一个WordPress插件并尝试从WooCommerce数据库中选择一些值,而我一直得到的是同样的错误。什么可能是错的?

我的代码:

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>";
    }       
}

错误:

  

致命错误:未捕获错误:在C:\ xampp \ htdocs \ ExamenProject \ wp-content \ plugins \ drukkebaasjes-sizedata \ drukkebaasjes-sizedata.php中调用未定义方法mysqli_result :: mysqli_fetch_assoc():54堆栈跟踪:# 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-&gt; apply_filters('',Array)#2 C:\ xampp \ htdocs \ ExamenProject \ wp-includes \ plugin.php(453):WP_Hook-&gt; do_action(Array)#3 C :\ xampp \ htdocs \ ExamenProject \ wp-admin \ admin.php(224):do_action('toplevel_page_p ...')#4 {main}抛出C:\ xampp \ htdocs \ ExamenProject \ wp-content \ plugins \第54行的drukkebaasjes-sizedata \ drukkebaasjes-sizedata.php

1 个答案:

答案 0 :(得分:2)

使用Wordpress,WPDB class and methods就是为了这个目的。请尝试以下方法:

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>";
    }
}

此功能代码现在无错误地工作。