重写SQL选择使用$ wpdb->准备

时间:2017-07-01 07:36:22

标签: mysql wordpress

我尝试重写SQL select查询以使用$wpdb->prepare函数。我原来的SQL查询看起来像这样,工作正常。

function hfwp_SaveFormData($formData, $userID, $today, $dbTable) {
   global $wpdb;

   $wpdb->show_errors();
   $query = "SELECT UserID FROM $dbTable WHERE Date = '$today' AND UserID = '$userID'";
   $wpdb->get_results( $query );
   $count = $wpdb->num_rows;

   if ($count > 0) {
      $wpdb->update($dbTable, array('Distance' => $formData), array('Date' => $today, 'UserID' => $userID));
   }else{
      $wpdb->insert($dbTable, array('UserID' => $userID, 'Date' => $today, 'Distance' => $formData));
   }
}

然后我尝试重写SELECT语句以使用$wpdb->prepare

$wpdb->get_results( $wpdb->prepare("SELECT UserID FROM %s WHERE Date = %s AND UserID = %d", $dbTable, $today, $userID ) );

但是它在日志中给出了一个错误,但还不足以给我一个关于错误的提示。

我还尝试了$wpdb->prepare的另一个版本:

$query = "SELECT UserID FROM %s WHERE Date = %s AND UserID = %d";
$prepared = $wpdb->prepare( $query , $dbTable, $today, $userID );
$wpdb->get_results( $prepared );

0 个答案:

没有答案