繁体   English   中英

如何从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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM