Php API错误-遇到非数值
我正在使用wordpress版本4.9.6并尝试创建以下API: 但是,当通过 下面是我的最低可行示例:Php API错误-遇到非数值,php,wordpress,Php,Wordpress,我正在使用wordpress版本4.9.6并尝试创建以下API: 但是,当通过 下面是我的最低可行示例: <?php add_action('rest_api_init', 'productRoutes'); function productRoutes() { register_rest_route('product/v1', 'manageproduct', array( 'methods' => WP_REST_SERVER::READABLE,
<?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;" );
我在将参数插入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;" );
有什么建议可以解释我为什么会在这条线上出错吗
谢谢你的回复 问题是您使用了错误的运算符进行串联。在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;" );
您是否检查了
$data
是否有一个名为category
的键?数据库中的category
是什么类型?@alanfcm$category
是db@MedURL中的值已正确给定$data
有一个类别
值。Thx!有没有进一步的建议来确保此查询的安全性?下面是一篇关于如何防止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;" );