如何使用$ wpdb检查数据库wordpress中是否存在变量

时间:2016-10-15 18:08:12

标签: php mysql wordpress

在Function.php中,我想检查我的变量是否存在于wordpress数据库的表中,但它不起作用。

global $wpdb;
    $count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->pincode_distro1 WHERE pincode = $the_order->shipping_postcode"));
    if($count == 1) { echo 'distro 1'}
    else { echo 'distro 2'}

2 个答案:

答案 0 :(得分:0)

我猜你不需要$ wpdb-> prepare(),尝试过这种方式?

$count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->pincode_distro1 WHERE pincode = '$the_order->shipping_postcode'");

答案 1 :(得分:0)

从表名称中删除$wpdb。代码可以正常工作

来自

$count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->pincode_distro1 WHERE pincode = '$the_order->shipping_postcode'");

$count = $wpdb->get_var("SELECT COUNT(*) FROM pincode_distro1 WHERE pincode = '$the_order->shipping_postcode'");