如何从wordpress $ wpdb-> get_results()解析值?

时间:2015-03-02 11:33:34

标签: php wordpress

我正在使用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,但仍然坚持结果。

1 个答案:

答案 0 :(得分:0)

如果您期望单个结果字段,则应使用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;
}