Here i have this query, If I replace the value of %f and %d as 1 and 1, it will work in my phpmyadmin panel but I run this query through wpdb get_results with prepare it doesnt work. All the values are being properly passed but still I get null as output.
$f = $wc * (($ul + 100)/100);
$capabilities = $wpdb->prefix."capabilities";
$sql = "
SELECT u.*, up.*, up.pid, ( up.ros - up.soh ) / ( %f ) as qty_suggested
FROM $wpdb->users u
INNER JOIN wp_deals_users_products up
ON u.ID = up.user_id
INNER JOIN $wpdb->usermeta um
ON u.ID = um.user_id
WHERE up.pid = %d
AND um.meta_key = '$capabilities'
AND um.meta_value LIKE '%subscriber%'
";
//echo $sql; This echoes the sql which I tested in phpmyadmin sql query runner
$results = $wpdb->get_results(
$wpdb->prepare(
$sql,
$f,
$pid
)
);
echo json_encode($results);
If I remove prepare and run query directly or with hardcoded 1 as both values for %d and %f I still get null. Same query will work fine in phpmyadmin sql section
EDIT:
This is the final query that goes in wpdb prepare function:
SELECT u.*, up.*, up.pid, ( up.ros - up.soh ) / ( %f ) as qty_suggested
FROM wp_users u
INNER JOIN wp_deals_users_products up
ON u.ID = up.user_id
INNER JOIN wp_usermeta um
ON u.ID = um.user_id
WHERE up.pid = %d
AND um.meta_key = 'wp_capabilities'
AND um.meta_value LIKE '%subscriber%'

