Php 选择2 Ajax查询不检索结果

Php 选择2 Ajax查询不检索结果,php,mysql,ajax,wordpress,jquery-select2,Php,Mysql,Ajax,Wordpress,Jquery Select2,我使用select2和ajax在数据库中查询特定分类下的术语,但是当我搜索时,搜索框只是挂起“搜索”,没有检索任何结果 这是我的html <select multiple="" name="regions1[]" id="regions1" class="job-manager-multiselect select2-hidden-accessible" required="" tabindex="-1" aria-hidden="true"></select> 我

我使用select2和ajax在数据库中查询特定分类下的术语,但是当我搜索时,搜索框只是挂起“搜索”,没有检索任何结果

这是我的html

<select multiple="" name="regions1[]" id="regions1" class="job-manager-multiselect select2-hidden-accessible" required="" tabindex="-1" aria-hidden="true"></select>

我的jquery:

<script>
jQuery(function($) {
$(document).ready(function() {
$( "#regions1" ).select2({        
ajax: {
    url: "/ajax/connect.php",
    dataType: 'json',
    delay: 250,
    data: function (params) {
        return {
            q: params.term // search term
        };
    },
    processResults: function (data) {
        // parse the results into the format expected by Select2.
        // since we are using custom formatting functions we do not need to
        // alter the remote JSON data
        return {
            results: data
        };
    },
    cache: true
},
minimumInputLength: 2
  });
  });
   });
 </script>
<?php

  $servername = "localhost";
  $username = "myusername";
  $password = "mypassword";

   try {
$conn = new PDO("mysql:host=$servername;dbname=mydatabase", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   }
    catch(PDOException $e)
   {
   echo "Connection failed: " . $e->getMessage();
   }



  // strip tags may not be the best method for your project to apply extra 
   layer of security but fits needs for this tutorial 
   $search = strip_tags(trim($_GET['q'])); 

 // Do Prepared Query
   $query = $conn->prepare("
   SELECT * FROM (
    SELECT wp_terms.name
   FROM wp_terms
   JOIN wp_term_taxonomy
    ON wp_term_taxonomy.term_id = wp_terms.term_id
    WHERE taxonomy = 'job_listing_region'
    AND count = 0
    ) as T"
     );

    // Add a wildcard search to the search variable
     $query->execute(array(':search'=>"%".$search."%"));


   // Do a quick fetchall on the results
    $list = $query->fetchall(PDO::FETCH_ASSOC);

   // Make sure we have a result
   if(count($list) > 0){
    foreach ($list as $key => $value) {
    $data[] = array('id' => $value['name'], 'text' => $value['name']);              
   } 
    } else {
   $data[] = array('id' => '0', 'text' => 'No Products Found');
   }


// return the result in json
echo json_encode($data);

jQuery(函数($){
$(文档).ready(函数(){
$(“#区域1”)。选择2({
阿贾克斯:{
url:“/ajax/connect.php”,
数据类型:“json”,
延误:250,
数据:函数(参数){
返回{
q:params.term//搜索词
};
},
processResults:函数(数据){
//将结果解析为Select2所需的格式。
//因为我们使用的是自定义格式函数,所以不需要
//更改远程JSON数据
返回{
结果:数据
};
},
缓存:真
},
最小输入长度:2
});
});
});
我的php代码查询数据库,我希望得到分类法“job\u listing\u region”下的所有术语名称。

<script>
jQuery(function($) {
$(document).ready(function() {
$( "#regions1" ).select2({        
ajax: {
    url: "/ajax/connect.php",
    dataType: 'json',
    delay: 250,
    data: function (params) {
        return {
            q: params.term // search term
        };
    },
    processResults: function (data) {
        // parse the results into the format expected by Select2.
        // since we are using custom formatting functions we do not need to
        // alter the remote JSON data
        return {
            results: data
        };
    },
    cache: true
},
minimumInputLength: 2
  });
  });
   });
 </script>
<?php

  $servername = "localhost";
  $username = "myusername";
  $password = "mypassword";

   try {
$conn = new PDO("mysql:host=$servername;dbname=mydatabase", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   }
    catch(PDOException $e)
   {
   echo "Connection failed: " . $e->getMessage();
   }



  // strip tags may not be the best method for your project to apply extra 
   layer of security but fits needs for this tutorial 
   $search = strip_tags(trim($_GET['q'])); 

 // Do Prepared Query
   $query = $conn->prepare("
   SELECT * FROM (
    SELECT wp_terms.name
   FROM wp_terms
   JOIN wp_term_taxonomy
    ON wp_term_taxonomy.term_id = wp_terms.term_id
    WHERE taxonomy = 'job_listing_region'
    AND count = 0
    ) as T"
     );

    // Add a wildcard search to the search variable
     $query->execute(array(':search'=>"%".$search."%"));


   // Do a quick fetchall on the results
    $list = $query->fetchall(PDO::FETCH_ASSOC);

   // Make sure we have a result
   if(count($list) > 0){
    foreach ($list as $key => $value) {
    $data[] = array('id' => $value['name'], 'text' => $value['name']);              
   } 
    } else {
   $data[] = array('id' => '0', 'text' => 'No Products Found');
   }


// return the result in json
echo json_encode($data);

$query=$smnt->prepare(…
我看不到任何地方定义了
$smnt
。我敢打赌你收到了PHP错误,你的服务器报告它们为404。在打开PHP错误进行调试的情况下,尝试在浏览器中点击
/ajax/connect.PHP
。@mopo922你是对的,我收到了“语法错误,意外的“$query”(t_变量)wp content/ajax/connect.php在第33行,有什么建议吗?
try
后面应该始终跟一个
catch
块-请参阅以了解有效的try/catch实现,您应该使用
$conn->prepare
而不是
$smnt->prepare
:search
参数绑定到查询,但查询不包括那个参数,我想这是你的问题