为什么jquery autocomplete不能处理mysql数据

unhi4e5o  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(370)

我有这个密码
function.js文件

$(function() {
    function split( val ) {
        return val.split( /,\s*/ );
    }
    function extractLast( term ) {
        return split( term ).pop();
    }

    $( ".country_suggestion" ).bind( "keydown", function( event ) {
        if ( event.keyCode === $.ui.keyCode.TAB &&
            $( this ).autocomplete( "instance" ).menu.active ) {
            event.preventDefault();
        }
    })
        .autocomplete({
            minLength: 1,
            source: function( request, response ) {
                $.ajax({
                    type: "POST",
                    url: "/suggestion.php",
                    data:'term='+request.term,
                    success: function(r){

                        return  r;
                        //response(r);
                    },
                    error: function (request, status, error) {
                    }
                });
                // delegate back to autocomplete, but extract the last term

            },
            focus: function() {
                // prevent value inserted on focus
                return false;
            },
            select: function( event, ui ) {
                var terms = split( this.value );
                // remove the current input
                terms.pop();
                // add the selected item
                terms.push( ui.item.value );
                // add placeholder to get the comma-and-space at the end
                terms.push( "" );
                this.value = terms.join( ", " );
                return false;
            }
        });
});

在php文件中

<?php

include('inc/safePDO.class.php');
include('inc/config.php');
$dbh = new 

  SafePDO('mysql:host='.$db['host'].';
dbname='.$db['name'].';charset=UTF8', $db['user'], $db['password']);

$term = $_POST['term'];

$list = $dbh->prepare("SELECT * FROM ((
                      SELECT `id`, `label` as name, CHAR_LENGTH(`label`) as name_length FROM `country`
                      WHERE `label` LIKE :name
                     ) UNION ( 
                      SELECT `id`, `label` as name, CHAR_LENGTH(`label`) as name_length FROM `countryregion`
                      WHERE `label` LIKE :name
                     )) as t
                     GROUP BY `name`
                     ORDER BY name_length ASC
                     LIMIT 0, 10");

$list->execute(array(':name' => "%{$term}%"));

$country_list = $list->fetchAll(PDO::FETCH_OBJ);

$country = array();
foreach($country_list as $country_region_list) {
$country[] = $country_region_list->name;
}
//print_r($country_list);
echo json_encode($country);

我几乎一路都在谷歌上搜索,但没有得到结果。
我得到的数据从数据库排序的形式,但唯一的事情是不工作的弹出窗口。。aucomplete弹出窗口未显示。。。
如果我在ajax的成功打印警报,它会显示短数据,但在那之后我不知道该怎么办。。。任何帮助都将不胜感激。

jecbmhm3

jecbmhm31#

pdo不允许多个占位符,请参见此问题
您可以更改代码重命名参数而不重复:

$list = $dbh->prepare("SELECT * FROM ((
                  SELECT `id`, `label` as name, CHAR_LENGTH(`label`) as name_length FROM `country`
                  WHERE `label` LIKE :name1
                 ) UNION ( 
                  SELECT `id`, `label` as name, CHAR_LENGTH(`label`) as name_length FROM `countryregion`
                  WHERE `label` LIKE :name2
                 )) as t
                 GROUP BY `name`
                 ORDER BY name_length ASC
                 LIMIT 0, 10");

$list->execute(array(':name1' => "%{$term}%",':name2' => "%{$term}%"));

相关问题