duandaishi9268 2018-06-01 15:17
浏览 37
已采纳

API错误 - 遇到非数字值

I am using wordpress Version 4.9.6 and trying to create the following API:

However, when accessing the api via

http://localhost/demo_wordpress_api/wp-json/product/v1/manageproduct?category=computer

Find below my minimum viable example:

<?php
add_action('rest_api_init', 'productRoutes');

function productRoutes()
{
    register_rest_route('product/v1', 'manageproduct', array(
        'methods' => WP_REST_SERVER::READABLE,
        'callback' => 'allproductitability',
    ));
}

function allProductsByCategory($data)
{
    global $wpdb;

    // show db errors
    $wpdb->show_errors(true);
    $wpdb->print_error();

    // $data['term']

    $mainQuery = $wpdb->get_results( "SELECT *
    FROM wp_product_API
    WHERE id IN(
        SELECT MAX(id)
        FROM wp_product_API
        WHERE category = \" " + $data['category'] + " \"
        GROUP BY id)  
    ORDER BY price DESC
    LIMIT 1;" );

When I run this I get the following error:

Warning: A non-numeric value encountered

I get the error where I am inserting my parameter into my SQL Query:

    $mainQuery = $wpdb->get_results( "SELECT *
    FROM wp_product_API
    WHERE id IN(
        SELECT MAX(id)
        FROM wp_product_API
        WHERE category = \" " + $data['category'] + " \" // ON THIS LINE I GET THE ERROR
        GROUP BY id)  
    ORDER BY price DESC
    LIMIT 1;" );

Any suggestions why I get the error on this line?

I appreciate your replies!

  • 写回答

1条回答 默认 最新

  • douhuang5331 2018-06-01 15:31
    关注

    The problem is that you are using the wrong operator for concatenation. In PHP . is what you should use like this:

    $mainQuery = $wpdb->get_results( "SELECT *
    FROM wp_product_API
    WHERE id IN(
        SELECT MAX(id)
        FROM wp_product_API
        WHERE category = \" " . $data['category'] . " \" // ON THIS LINE I GET THE ERROR
        GROUP BY id)  
    ORDER BY price DESC
    LIMIT 1;" );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端