wordpress - $wpdb query not working in plugin -
i'm trying reviews plugin in wordpress, , 1 of thing client asked me user should able rate 1 product every 24 horas, have establish limit between rates.
and made function check if limit reached:
function isallowedtorate(){ global $wpdb; $currentdate = date('y-m-d'); $userid = get_current_user_id(); $table_name = $wpdb->prefix .'esvn_reviews'; $isallowed = $wpdb->get_results( " select user_id, fecha_calificacion {$table_name} user_id = {$userid} , review_date = {$currentdate} " ); if( count( $isallowed > 0 ) ){ return false; }else{ return true; } }
but every time try run it, returns false, error i'm getting wordpress this:
<div id='error'> <p class='wpdberror'><strong>wordpress database error:</strong> []<br /> <code> select user_id, fecha_calificacion wp_esvn_reviews user_id = 6 , fecha_calificacion = 2015-10-30 </code></p> </div>
if take same sql , run directly database, works charm, keep getting error wordpress , function returns false.
you need wrap date in single quotes.
$isallowed = $wpdb->get_results( " select user_id, fecha_calificacion {$table_name} user_id = {$userid} , review_date = '{$currentdate}' " );
alternatively use $wpdb->prepare()
, consider better form.
$sql = <<<sql select user_id, fecha_calificacion {$table_name} user_id = %d -- formatted (d)igit , review_date = %s -- formatted (s)tring sql; $isallowed = $wpdb->get_results($wpdb->prepare($sql, $userid, $currentdate));
Comments
Post a Comment