亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 .module 文件中的靜態 SQL 查詢中傳遞參數 - Drupal 8

在 .module 文件中的靜態 SQL 查詢中傳遞參數 - Drupal 8

PHP
滄海一幻覺 2022-08-19 15:51:15
在我的Drupal 8站點中,我有一個“搜索”頁面功能。為此,我正在嘗試將我的自定義SQL查詢與核心Drupal搜索查詢連接起來。我正在嘗試向.module文件中的SQL查詢添加參數,但它向我拋出了語法錯誤。我的查詢如下:function mymodule_views_query_alter(\Drupal\views\ViewExecutable $view, \Drupal\views\Plugin\views\query\QueryPluginBase $query) {//If my view is my-search and the Miles field is not empty and not 'Any' and zipcode field is not empty  $searched_miles_value = $view->getRequest()->get('field_miles_value');  $searched_zip_value = $view->getRequest()->get('field_zip_value');  if(!empty($searched_miles_value)) {    _remove_view_query_condition($query, 'node__field_miles.field_miles_value');  }   if(!empty($searched_zip_value)) {    _remove_view_query_condition($query, 'node__field_zip.field_zip_value');  }  if($view->id() == 'my_search'  &&       !empty($searched_miles_value) &&      !empty($searched_zip_value) &&      ($searched_miles_value != 'any')) {        $connection = \Drupal\Core\Database\Database::getConnection();        $result = $connection->query('SELECT to_zip FROM zipmaster_xref WHERE from_zip = [field_zip_value] AND miles = [field_miles_value]')->fetchAll();        $target_zips = ['98567'];//hardcoded for testing        foreach($result as $zip) {          $target_zips[] = $zip->to_zip;        }        $query->addWhere('new_group', 'node__field_zip.field_zip_value', $target_zips, 'IN');  }我在SQL查詢行上遇到語法錯誤。有什么幫助嗎?
查看完整描述

1 回答

?
慕俠2389804

TA貢獻1719條經驗 獲得超6個贊

嘗試在查詢中使用變量:

$result = $connection->query("SELECT to_zip FROM zipmaster_xref WHERE from_zip = '".$searched_zip_value."' AND miles = '".$searched_miles_value."'")->fetchAll();


查看完整回答
反對 回復 2022-08-19
  • 1 回答
  • 0 關注
  • 102 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號