朋友们,请您帮助我进行以下sql查询尝试。此查询控制一个人是否已执行表单操作24小时。
这是SQL:
SELECT min(TIMESTAMPDIFF(DAY, `fecha_inscripcion`, now())) FROM `wp_cf7dbplugin_submits` WHERE `field_name`="cedula" and `field_value` = "1144093762"
结果如下:
min(TIMESTAMPDIFF(DAY, `fecha_inscripcion`, now()))
5
现在,在WordPress中我这样做了
global $wpdb;
$day = \'DAY\';
$now = \'now()\';
$fieldvalue = \'1144093762\';
$fieldname = \'cedula\';
$post_count = $wpdb->get_var("
SELECT min(TIMESTAMPDIFF($day,$wpdb->cf7dbplugin_submits.fecha_inscripcion, $now))
FROM $wpdb->cf7dbplugin_submits
WHERE field_name=$fieldname and field_value=$fieldvalue");
print_r($post_count);
echo "Resultado"+$post_count;
但它在0中返回我,在本例中应该是5。
感谢
SO网友:Paul \'Sparrow Hawk\' Biron
主要问题是如何指定自定义表名(例如。,$wpdb->cf7dbplugin_submits
).
$wpdb
只有通过访问表名时才知道“内置”表$wpdb->xxx
. 要指定访问自定义表名,请使用{$wpdb->prefix}custom_table_name
.
我注意到的另一件事是,为了安全起见,您应该never 在传递给任何$wpdb
查询方法。相反,您应该使用$wpdb->prepare().
将这两件事放在一起会导致:
$sql = $wpdb->prepare (
"SELECT min(TIMESTAMPDIFF($day,`fecha_inscripcion`, $now))
FROM {$wpdb->prefix}cf7dbplugin_submits
WHERE field_name=%s and field_value=%s",
$fieldname,
$fieldvalue
) ;
$post_count = $wpdb->get_var ($sql) ;
SO网友:iDuruiz
我附上解决方案,谢谢Paul“Sparrow Hawk”Biron
global $wpdb;
$day = \'DAY\';
$now = \'now()\';
$fieldname = "cedula";
$fieldvalue = "1144092";
// set the meta_key to the appropriate custom field meta key
$sql = $wpdb->prepare (
"SELECT min(TIMESTAMPDIFF($day,fecha_inscripcion, $now))
FROM {$wpdb->prefix}cf7dbplugin_submits
WHERE field_name=%s and field_value=%s",
$fieldname,
$fieldvalue
);
$post_count = $wpdb->get_var($sql) ;
echo "<p>User count is {$post_count}</p>";