我正在使用wordpress版本4.9.6并尝试创建以下API:
但是,通过
访问api时http://localhost/demo_wordpress_api/wp-json/product/v1/manageproduct?category=computer
在下面找到我最不可行的例子:
<?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;" );
当我运行时,我收到以下错误:
Warning: A non-numeric value encountered
我在将SQL参数插入SQL查询时遇到错误:
$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;" );
为什么我在这一行收到错误的任何建议?
感谢您的回复!
答案 0 :(得分:1)
问题是您使用错误的运算符进行连接。在PHP .
中你应该这样使用:
$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;" );