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 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝