mysqli\u fetch\u assoc尝试从数据库中选择值时出错

aij0ehis  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(289)

我目前正在制作一个wordpress插件,并试图从woomcommerce数据库中选择一些值,我一直得到的是相同的错误。有什么问题吗?
我的代码:

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中未定义的方法mysqli\u result::mysqli\u fetch\u assoc()。php:54 stack 跟踪:#0 c:\xampp\htdocs\examenproject\wp includes\class wp hook.php(286):prntpage('')#1c:\xampp\htdocs\examenproject\wp includes\class wp hook.php(310):wp\u hook->应用\u过滤器(“”,array)#2 c:\xampp\htdocs\examenproject\wp includes\plugin.php(453):wp\u hook->do\u action(array)#3 c:\xampp\htdocs\examenproject\wp admin\admin.php(224):do\u action('toplevel\u page\u p…')#4{main}在第54行的c:\xampp\htdocs\examenproject\wp content\plugins\drukkebasjes sizedata\drukkebasjes-sizedata.php中抛出

col17t5w

col17t5w1#

wordpress提供了用于此目的的wpdb类和方法。请尝试以下操作:

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

这个函数代码现在可以正常工作了。

相关问题