如何解析wordpress$wpdb中的值->;get_results()


How to parse value from wordpress $wpdb->get_results()?

我正在处理wordpress主题,未能在wpdb->get_results()上使用算术运算。

如果我写得简单,那么它在数据库中成功更新

$sql = "SELECT room_count from " . BOOKYOURTRAVEL_ACCOMMODATION_VACANCIES_TABLE . " where accommodation_id = $accommodation_id";
    $result = $wpdb->get_results($sql) or die(mysql_error()); 

    $calc = $result;

但如果我对结果使用减号运算,就会抛出致命的错误。

$sql = "SELECT room_count from " . BOOKYOURTRAVEL_ACCOMMODATION_VACANCIES_TABLE . " where accommodation_id = $accommodation_id";
    $result = $wpdb->get_results($sql) or die(mysql_error()); 

    $calc = $result - 1;

错误

Fatal error: Unsupported operand types in C:'xampp'htdocs'beta'wp-content'themes'byt-child'includes'post_types'accommodations.php on line 1199

Freinds请建议我如何解决这个问题。我使用了result->room_count-1,但在结果上卡住了。

如果您期望一个结果字段,则应该使用get_var

$sql = "SELECT room_count from " . BOOKYOURTRAVEL_ACCOMMODATION_VACANCIES_TABLE . " where accommodation_id = $accommodation_id";
$result = $wpdb->get_var($sql); 
if( $result ) {
    $calc = $result - 1;
}