dongyuan9292 2016-01-27 16:20
浏览 8
已采纳

从$ wpdb获得正确的价值

Before creating new post programmatically I want to check if there is a post with the same headline in my database.

I am checking that like this:

$results = $wpdb->get_results("SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'candidates' AND post_title = '$name'");

Example of value I get when I var_dump($results):

array(1) { [0]=> object(stdClass)#85 (1) { ["COUNT(*)"]=> string(1) "8" } }

Now I want to get that string(1) "8", add it to variable and convert it to number, but I am not sure how to get it.

I tried something like

$number = (int)$results[0]->COUNT(*) 

but that isn't right. I also tried some other combinations but couldn't figure it out

  • 写回答

1条回答 默认 最新

  • drcmg28484 2016-01-27 16:34
    关注

    Your very problem is the (*) in $number = (int)$results[0]->COUNT(*). You cannot use these special characters simply as 'variable'. You're better off using a column alias using AS your_column_alias.

    So you want to use

    SELECT COUNT(*) as post_count FROM $wpdb->posts  WHERE post_type = 'candidates' AND post_title = '$name'
    

    as query and

    $number = (int) $results[0]->post_count;
    

    to fetch the aliased column from your resultset.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度